coder
coder

Reputation: 97

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

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

Upvotes: 2

Views: 18001

Answers (1)

demoncodemonkey
demoncodemonkey

Reputation: 11957

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.

Upvotes: 2

Related Questions