I am marching forward very well but got stuck in here while adding data transfer objects (.cs files), this code is not working in ProductRepository class-
public class ProductRepository : IProductRepository
{
/// <summary>
/// IQueryable of all Products
/// </summary>
/// <returns></returns>
public IQueryable<Product> GetProducts()
{
var dataContext = new NorthwindEntities();
var products = from p in dataContext.Products
select p;
return products;
}
/// <summary>
/// IQueryable of Projects projected
/// into the ProductViewModel class
/// </summary>
/// <returns></returns>
public IQueryable<ProductViewModel> GetProductsProjected(int? supplierID, int? categoryID)
{
var projectedProducts = from p in GetProducts()
select new ProductViewModel
{
ProductID = p.ProductID,
ProductName = p.ProductName,
UnitPrice = p.UnitPrice,
CategoryName = p.Category.CategoryName,
CategoryID = p.CategoryID,
SupplierID = p.SupplierID,
Discontinued = p.Discontinued
};
// Filter on SupplierID
if (supplierID.HasValue)
{
projectedProducts = projectedProducts.Where(a => a.SupplierID == supplierID);
}
// Filter on CategoryID
if (categoryID.HasValue)
{
projectedProducts = projectedProducts.Where(a => a.CategoryID == categoryID);
}
return projectedProducts;
}
public IQueryable<SupplierViewModel> GetSuppliers()
{
var dataContext = new NorthwindEntities();
var suppliers = from s in dataContext.Suppliers
select new SupplierViewModel
{
SupplierID = s.SupplierID,
CompanyName = s.CompanyName
};
return suppliers;
}
This is the error-
1 'ProductRepository': member names cannot be the same as their enclosing type.
That is generally caused by a class containing a property or function with the same name. The two examples below throw the same compiler message when built. Are there any other properties or functions in that class with the same name? I didn't see any
the post, but it is not clear if that is the entire file. Also, could you give the line and class name the compiler is listing the error on?
public class SomeClass
{
public Int32 SomeClass { get; set; }
}
public class SomeClass
{
public void SomeClass()
{
}
}
Also, could you give the line and class name the compiler is listing the error on?
Sure.. Here is the enitre ProductRepository.cs class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Reporting.WebForms;
namespace NorthwindReports.DAL
{
public class ProductRepository
{
public class ProductRepository : IProductRepository
{
/// <summary>
/// IQueryable of all Products
/// </summary>
/// <returns></returns>
public IQueryable<Product> GetProducts()
{
var dataContext = new NorthwindEntities();
var products = from p in dataContext.Products
select p;
return products;
}
/// <summary>
/// IQueryable of Projects projected
/// into the ProductViewModel class
/// </summary>
/// <returns></returns>
public IQueryable<ProductViewModel> GetProductsProjected(int? supplierID, int? categoryID)
{
var projectedProducts = from p in GetProducts()
select new ProductViewModel
{
ProductID = p.ProductID,
ProductName = p.ProductName,
UnitPrice = p.UnitPrice,
CategoryName = p.Category.CategoryName,
CategoryID = p.CategoryID,
SupplierID = p.SupplierID,
Discontinued = p.Discontinued
};
// Filter on SupplierID
if (supplierID.HasValue)
{
projectedProducts = projectedProducts.Where(a => a.SupplierID == supplierID);
}
// Filter on CategoryID
if (categoryID.HasValue)
{
projectedProducts = projectedProducts.Where(a => a.CategoryID == categoryID);
}
return projectedProducts;
}
public IQueryable<SupplierViewModel> GetSuppliers()
{
var dataContext = new NorthwindEntities();
var suppliers = from s in dataContext.Suppliers
select new SupplierViewModel
{
SupplierID = s.SupplierID,
CompanyName = s.CompanyName
};
return suppliers;
}
public IQueryable<CategoryViewModel> GetCategories()
{
var dataContext = new NorthwindEntities();
var categories = from c in dataContext.Categories
select new CategoryViewModel
{
CategoryID = c.CategoryID,
CategoryName = c.CategoryName
};
return categories;
}
}
}
}
Error:
Error 1 'ProductRepository': member names cannot be the same as their enclosing type C:\Users\USER\Documents\Visual Studio 2010\Projects\NorthwindReports\NorthwindReports\DAL\ProductRepository.cs 12 22 NorthwindReports
I tried removing the outer class.. But then it gave me 4 errors-
Error 1 'NorthwindReports.DAL.IProductRepository.GetProducts()' must declare a body because it is not marked abstract, extern, or partial C:\Users\USER\Documents\Visual Studio 2010\Projects\NorthwindReports\NorthwindReports\DAL\IProductRepository.cs 10
29 NorthwindReports
Error 2 'NorthwindReports.DAL.IProductRepository.GetProductsProjected(int?, int?)' must declare a body because it is not marked abstract, extern, or partial C:\Users\USER\Documents\Visual Studio 2010\Projects\NorthwindReports\NorthwindReports\DAL\IProductRepository.cs
11 38 NorthwindReports
Error 3 'NorthwindReports.DAL.IProductRepository.GetSuppliers()' must declare a body because it is not marked abstract, extern, or partial C:\Users\USER\Documents\Visual Studio 2010\Projects\NorthwindReports\NorthwindReports\DAL\IProductRepository.cs 12
39 NorthwindReports
Error 4 'NorthwindReports.DAL.IProductRepository.GetCategories()' must declare a body because it is not marked abstract, extern, or partial C:\Users\USER\Documents\Visual Studio 2010\Projects\NorthwindReports\NorthwindReports\DAL\IProductRepository.cs
13 39 NorthwindReports
The issue is the double class definition. Does this work?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Reporting.WebForms;
namespace NorthwindReports.DAL
{
public class ProductRepository : IProductRepository
{
/// <summary>
/// IQueryable of all Products
/// </summary>
/// <returns></returns>
public IQueryable<Product> GetProducts()
{
var dataContext = new NorthwindEntities();
var products = from p in dataContext.Products
select p;
return products;
}
/// <summary>
/// IQueryable of Projects projected
/// into the ProductViewModel class
/// </summary>
/// <returns></returns>
public IQueryable<ProductViewModel> GetProductsProjected(int? supplierID, int? categoryID)
{
var projectedProducts = from p in GetProducts()
select new ProductViewModel
{
ProductID = p.ProductID,
ProductName = p.ProductName,
UnitPrice = p.UnitPrice,
CategoryName = p.Category.CategoryName,
CategoryID = p.CategoryID,
SupplierID = p.SupplierID,
Discontinued = p.Discontinued
};
// Filter on SupplierID
if (supplierID.HasValue)
{
projectedProducts = projectedProducts.Where(a => a.SupplierID == supplierID);
}
// Filter on CategoryID
if (categoryID.HasValue)
{
projectedProducts = projectedProducts.Where(a => a.CategoryID == categoryID);
}
return projectedProducts;
}
public IQueryable<SupplierViewModel> GetSuppliers()
{
var dataContext = new NorthwindEntities();
var suppliers = from s in dataContext.Suppliers
select new SupplierViewModel
{
SupplierID = s.SupplierID,
CompanyName = s.CompanyName
};
return suppliers;
}
public IQueryable<CategoryViewModel> GetCategories()
{
var dataContext = new NorthwindEntities();
var categories = from c in dataContext.Categories
select new CategoryViewModel
{
CategoryID = c.CategoryID,
CategoryName = c.CategoryName
};
return categories;
}
}
}
From what you have posted, your interface looks fine, but we can see the compiler errors are coming from the interface class. Could you post the whole cs file for the IProductRepository. The code below compiles fine for me. I had to replace the calsses
I did not have with Int32, but that shouldn't matter.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary1
{
interface IProductRepository
{
IQueryable<Int32> GetProducts();
IQueryable<Int32> GetProductsProjected(int? supplierID, int? categoryID);
IQueryable<Int32> GetSuppliers();
IQueryable<Int32> GetCategories();
}
}
Could you post the whole cs file for the IProductRepository.
Sure .. Here it is.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace NorthwindReports.DAL
{
interface IProductRepository
{
IQueryable<Int32> GetProducts();
IQueryable<Int32> GetProductsProjected(int? supplierID, int? categoryID);
IQueryable<Int32> GetSuppliers();
IQueryable<Int32> GetCategories();
}
}
But before this when I included 'usingMicrosoft.Reporting.WebForms; ' in the ProductRepository.cs, I am getting the following error-
Error 1
The type or namespace name 'Reporting' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
C:\Users\USER\documents\visual studio 2010\Projects\NorthwindReports\NorthwindReports\DAL\ProductRepository.cs 5 17 NorthwindReports
girizh
Member
302 Points
404 Posts
ProductRepository error..
Jun 28, 2012 05:01 PM|LINK
Hi all,
Trying to create a report by pratices and got stuck here. One of the expert suggested this link-
http://weblogs.asp.net/rajbk/archive/2010/05/09/creating-an-asp-net-report-using-visual-studio-2010-part-1.aspx
I am marching forward very well but got stuck in here while adding data transfer objects (.cs files), this code is not working in ProductRepository class-
public class ProductRepository : IProductRepository { /// <summary> /// IQueryable of all Products /// </summary> /// <returns></returns> public IQueryable<Product> GetProducts() { var dataContext = new NorthwindEntities(); var products = from p in dataContext.Products select p; return products; } /// <summary> /// IQueryable of Projects projected /// into the ProductViewModel class /// </summary> /// <returns></returns> public IQueryable<ProductViewModel> GetProductsProjected(int? supplierID, int? categoryID) { var projectedProducts = from p in GetProducts() select new ProductViewModel { ProductID = p.ProductID, ProductName = p.ProductName, UnitPrice = p.UnitPrice, CategoryName = p.Category.CategoryName, CategoryID = p.CategoryID, SupplierID = p.SupplierID, Discontinued = p.Discontinued }; // Filter on SupplierID if (supplierID.HasValue) { projectedProducts = projectedProducts.Where(a => a.SupplierID == supplierID); } // Filter on CategoryID if (categoryID.HasValue) { projectedProducts = projectedProducts.Where(a => a.CategoryID == categoryID); } return projectedProducts; } public IQueryable<SupplierViewModel> GetSuppliers() { var dataContext = new NorthwindEntities(); var suppliers = from s in dataContext.Suppliers select new SupplierViewModel { SupplierID = s.SupplierID, CompanyName = s.CompanyName }; return suppliers; }This is the error-
Please help.
Regards,
Mark As Answer if it helps you :)
MattsDotNetU...
Contributor
3178 Points
515 Posts
Re: ProductRepository error..
Jun 28, 2012 05:21 PM|LINK
That is generally caused by a class containing a property or function with the same name. The two examples below throw the same compiler message when built. Are there any other properties or functions in that class with the same name? I didn't see any the post, but it is not clear if that is the entire file. Also, could you give the line and class name the compiler is listing the error on?
public class SomeClass { public Int32 SomeClass { get; set; } }public class SomeClass { public void SomeClass() { } }girizh
Member
302 Points
404 Posts
Re: ProductRepository error..
Jun 28, 2012 05:30 PM|LINK
Sure.. Here is the enitre ProductRepository.cs class
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.Reporting.WebForms; namespace NorthwindReports.DAL { public class ProductRepository { public class ProductRepository : IProductRepository { /// <summary> /// IQueryable of all Products /// </summary> /// <returns></returns> public IQueryable<Product> GetProducts() { var dataContext = new NorthwindEntities(); var products = from p in dataContext.Products select p; return products; } /// <summary> /// IQueryable of Projects projected /// into the ProductViewModel class /// </summary> /// <returns></returns> public IQueryable<ProductViewModel> GetProductsProjected(int? supplierID, int? categoryID) { var projectedProducts = from p in GetProducts() select new ProductViewModel { ProductID = p.ProductID, ProductName = p.ProductName, UnitPrice = p.UnitPrice, CategoryName = p.Category.CategoryName, CategoryID = p.CategoryID, SupplierID = p.SupplierID, Discontinued = p.Discontinued }; // Filter on SupplierID if (supplierID.HasValue) { projectedProducts = projectedProducts.Where(a => a.SupplierID == supplierID); } // Filter on CategoryID if (categoryID.HasValue) { projectedProducts = projectedProducts.Where(a => a.CategoryID == categoryID); } return projectedProducts; } public IQueryable<SupplierViewModel> GetSuppliers() { var dataContext = new NorthwindEntities(); var suppliers = from s in dataContext.Suppliers select new SupplierViewModel { SupplierID = s.SupplierID, CompanyName = s.CompanyName }; return suppliers; } public IQueryable<CategoryViewModel> GetCategories() { var dataContext = new NorthwindEntities(); var categories = from c in dataContext.Categories select new CategoryViewModel { CategoryID = c.CategoryID, CategoryName = c.CategoryName }; return categories; } } } }Mark As Answer if it helps you :)
girizh
Member
302 Points
404 Posts
Re: ProductRepository error..
Jun 28, 2012 05:32 PM|LINK
I tried removing the outer class.. But then it gave me 4 errors-
Mark As Answer if it helps you :)
MattsDotNetU...
Contributor
3178 Points
515 Posts
Re: ProductRepository error..
Jun 28, 2012 05:43 PM|LINK
The issue is the double class definition. Does this work?
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.Reporting.WebForms; namespace NorthwindReports.DAL { public class ProductRepository : IProductRepository { /// <summary> /// IQueryable of all Products /// </summary> /// <returns></returns> public IQueryable<Product> GetProducts() { var dataContext = new NorthwindEntities(); var products = from p in dataContext.Products select p; return products; } /// <summary> /// IQueryable of Projects projected /// into the ProductViewModel class /// </summary> /// <returns></returns> public IQueryable<ProductViewModel> GetProductsProjected(int? supplierID, int? categoryID) { var projectedProducts = from p in GetProducts() select new ProductViewModel { ProductID = p.ProductID, ProductName = p.ProductName, UnitPrice = p.UnitPrice, CategoryName = p.Category.CategoryName, CategoryID = p.CategoryID, SupplierID = p.SupplierID, Discontinued = p.Discontinued }; // Filter on SupplierID if (supplierID.HasValue) { projectedProducts = projectedProducts.Where(a => a.SupplierID == supplierID); } // Filter on CategoryID if (categoryID.HasValue) { projectedProducts = projectedProducts.Where(a => a.CategoryID == categoryID); } return projectedProducts; } public IQueryable<SupplierViewModel> GetSuppliers() { var dataContext = new NorthwindEntities(); var suppliers = from s in dataContext.Suppliers select new SupplierViewModel { SupplierID = s.SupplierID, CompanyName = s.CompanyName }; return suppliers; } public IQueryable<CategoryViewModel> GetCategories() { var dataContext = new NorthwindEntities(); var categories = from c in dataContext.Categories select new CategoryViewModel { CategoryID = c.CategoryID, CategoryName = c.CategoryName }; return categories; } } }MattsDotNetU...
Contributor
3178 Points
515 Posts
Re: ProductRepository error..
Jun 28, 2012 05:45 PM|LINK
The issue may be with the interface too. Could you also post the code for IProductRepository if the previous idea did not work?
girizh
Member
302 Points
404 Posts
Re: ProductRepository error..
Jun 28, 2012 05:49 PM|LINK
This is the one-interface IProductRepository{Mark As Answer if it helps you :)
MattsDotNetU...
Contributor
3178 Points
515 Posts
Re: ProductRepository error..
Jun 28, 2012 06:03 PM|LINK
From what you have posted, your interface looks fine, but we can see the compiler errors are coming from the interface class. Could you post the whole cs file for the IProductRepository. The code below compiles fine for me. I had to replace the calsses I did not have with Int32, but that shouldn't matter.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ClassLibrary1 { interface IProductRepository { IQueryable<Int32> GetProducts(); IQueryable<Int32> GetProductsProjected(int? supplierID, int? categoryID); IQueryable<Int32> GetSuppliers(); IQueryable<Int32> GetCategories(); } }girizh
Member
302 Points
404 Posts
Re: ProductRepository error..
Jun 29, 2012 09:12 AM|LINK
Sure .. Here it is.
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace NorthwindReports.DAL { interface IProductRepository { IQueryable<Int32> GetProducts(); IQueryable<Int32> GetProductsProjected(int? supplierID, int? categoryID); IQueryable<Int32> GetSuppliers(); IQueryable<Int32> GetCategories(); } }But before this when I included 'using Microsoft.Reporting.WebForms; ' in the ProductRepository.cs, I am getting the following error-
Mark As Answer if it helps you :)