Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I am new to entity framework/mvc/asp and am running into an error I'm not sure how to resolve.

The error I am getting is Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<system.web.mvc.selectlistitem>' to 'FirmAdmin.Business.RegistrationType'

Here is the code throwing the error I am getting on
return oRegResults;


C#
public Business.RegistrationType GetReg()
        {
            //**********
            //Initialize
            //**********
            const string cMyMethodName = "GetFirm";
            string cContext = "Initializing";
            System.Linq.IQueryable<Data.Registration_Type> oFirmListEF;
            //System.Collections.Generic.List<Business.RegistrationType> oRegResults = new System.Collections.Generic.List<Business.RegistrationType>();
            List<SelectListItem> oRegResults = new List<SelectListItem>();

            try
            {
              

                using (var oFirmContext = new FirmContext(oDB, true))
                {

                   
                    List<SelectListItem> oRegList = new List<SelectListItem>();
                    var registrationList = (oFirmListEF = oFirmContext.Registration_Type.AsQueryable()
                    .OrderBy(f => f.Registration)).ToList();



                    //***************************
                    //Prepare a View Model object
                    //***************************
                    cContext = "Loading data from EF into view model";
                                      System.Collections.Generic.List<Business.RegistrationType> oReg = new System.Collections.Generic.List<Business.RegistrationType>();

                    foreach (var item in registrationList)
                    {
                       oRegList.Add(new SelectListItem { Value = item.Registration.ToString() });
                        
                    };


                     oRegResults = oRegList;

                   
                }

            }
            catch (Exception ex)
            {
                //****************
                //Report the error
                //****************
                throw new System.Exception("Error occured in " + this.MyClassName + "." + cMyMethodName + " Error: " + ex.Message.ToString() + " Context: " + cContext);
               
            }

            return oRegResults;
        }



Here is the code in RegistrationType
C#
<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace Fed.FirmAdmin.Business
{
    /// <summary>
    /// This class represents a Firm
    /// </summary>
    public class RegistrationType : BaseEntity
    {
        private const string MyClassName = "RegistrationType";

        [DisplayName("ID:")]  
        [Required]
        [Key]
        public int RegId { get; set; }


        [DisplayName("Registration:")]
        //[Required]
        public string Registration { get; set; }



}


What I have tried:

I am not sure what i am missing in RegistrationType is seems like I need to declare a list item there?
Posted
Updated 24-Mar-21 12:55pm

1 solution

You have declared the GetReg method as Business.RegistrationType - which means it returns a single instance of the class Business.RegistrationType.

To simplify it, you would get the same error with this code:
C#
private int Fail()
   {
   return "Hello world";
   }
Because Failis declared as returning a integer, and you are returning a string. You can't implicitly cast a string to an integer - because it might be the text "Hello world" and that doesn't have a numeric equivalent - so the sytem tells you you have made a mistake somewhere.

Your GetReg method returns a List<SelectListItem> which is a collection of SelectListItem objects, and even the individual instances have no obvious relation to the return type!

We can't fix that for you: I have no idea what the heck you expect that to return, and much less idea where it might get that information from!
 
Share this answer
 
Comments
Member 15053439 25-Mar-21 9:44am    
Hi Griff...your explaination was enough for me to realize my issues. Thank you for taking the time to give me a response.
OriginalGriff 25-Mar-21 10:06am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900