Cannot implicitly convert type. An explicit conversion exists (are you missing a cast?)

16,440

The error is saying that the compiler thinks that
ProductsController.GetAllProducts wants to return a WebApplication1.Product
but IProductRepository.GetAll returns a WebApplication1.Models.Product instead.

Do you have 2 Product classes in these 2 different namespaces?

Alternatively, maybe you previously had the Product class in the WebApplication1 namespace, and it's now been moved into the WebApplication1.Models namespace, maybe you just have a stale reference and a "Rebuild All" will fix it.

Share:
16,440
coder
Author by

coder

Updated on June 04, 2022

Comments

  • coder
    coder almost 2 years

    I am trying to develop a web application using ASP.Net Web API. Now after creating Product model and IProductRepository interface I'm having difficulty in showing all products. I'm getting error message

    Cannot implicitly convert type System.Collections.Generic.IEnumerable<WebApplication1.Models.Product> to System.Collections.Generic.IEnumerable<WebApplication1.Product>. An explicit conversion exists (are you missing a cast?)

    Controllers\ProductsController.cs 17 20 WebApplication1

    on this line of code

    return repo.GetAll();

    I don't understand what to do. If someone points out the problem and explain me what I'm doing wrong that would be very helpful.

    ProductsController.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using WebApplication1.Models;
    
    namespace WebApplication1.Controllers
    {
        public class ProductsController : ApiController
        {
            static readonly IProductRepository repo = new ProductRepository();
    
            public IEnumerable<Product> GetAllProducts()
            {
                return repo.GetAll();
            }
        }
    }
    

    ProductRepository.cs

    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    
    namespace WebApplication1.Models
    {
        public class ProductRepository : IProductRepository
        {
            public string cs = System.Configuration.ConfigurationManager.ConnectionStrings["MediaSoftAppEntities"].ConnectionString;
    
            public IEnumerable<Product> GetAll()
            {
                List<Product> products = new List<Product>();
                string sql = String.Format("Select [ProductID], [ProductName], [Price] FROM [Products]");
    
                using (SqlConnection con = new SqlConnection(cs)){
                    using (SqlCommand cmd = new SqlCommand(sql, con)){
                        con.Open();
                        SqlDataReader dr = cmd.ExecuteReader();
                        while(dr.Read()){
                            Product product = new Product();
                            product.ProductID = dr.GetInt32(0);
                            product.ProductName = dr.GetString(1);
                            product.Price = dr.GetInt32(2);
                        }
                    }
                }
                return products.ToArray();
            }
        }
    }
    

    IProductRepository.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WebApplication1.Models
    {
        public interface IProductRepository
        {
            IEnumerable<Product> GetAll();
        }
    }
    
  • coder
    coder almost 10 years
    The problem's solved, thanks to you. I wrongly added the ADO.Net entity Data Model in the root folder rather than in Model folder. So it auto generated 3 models in the root folder which I didn't notice. I manually added Product model so it raised the conflict. Now all working fine after deleting replicates and adding data models in model folder.