locked
ProductRepository error.. RRS feed

  • Question

  • User-1853867835 posted

    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-

    1 'ProductRepository': member names cannot be the same as their enclosing type.

    Please help.

    Regards,

    Thursday, June 28, 2012 1:01 PM

All replies

  • User-287491473 posted

    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()
            {
    
            }
        }



    Thursday, June 28, 2012 1:21 PM
  • User-1853867835 posted

    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
    
    
    




    Thursday, June 28, 2012 1:30 PM
  • User-1853867835 posted

    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
    



    Thursday, June 28, 2012 1:32 PM
  • User-287491473 posted

    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; 
                } 
            } 
     
     
    }

    Thursday, June 28, 2012 1:43 PM
  • User-287491473 posted

    The issue may be with the interface too.  Could you also post the code for IProductRepository if the previous idea did not work?

    Thursday, June 28, 2012 1:45 PM
  • User-1853867835 posted
    This is the one-

    interface IProductRepository
        {
            IQueryable<Product> GetProducts();
            IQueryable<ProductViewModel> GetProductsProjected(int? supplierID, int? categoryID);
            IQueryable<SupplierViewModel> GetSuppliers();
            IQueryable<CategoryViewModel> GetCategories();
        }
    Thursday, June 28, 2012 1:49 PM
  • User-287491473 posted

    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();
        }
    }

     

    Thursday, June 28, 2012 2:03 PM
  • User-1853867835 posted

    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 'using Microsoft.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
    
    



    Friday, June 29, 2012 5:12 AM