This is probably an age-old question but after a solid day of Googling I am yet to figure out the answer.
Ultimately, I have a drop-down that needs to be populated by a list based on one of my model classes. I have the corresponding data in a database and I am retrieving the data with a stored procedure. I can get the data out and into a List of type string > but it actually needs to go into a List of type MyParticularModel>. Below I have provided more details. Could anybody tell me what I am doing wrong?
Error message is “Cannot implicitly convert type‘ System.Collections.Generic.List String> ’ to ‘System.Collections.Generic.List TrackerLibrary.Models.PersonModel> ’”
The second to last line of code is where I am getting the error message:
public List<PersonModel> GetPerson_All()
{
//spPeople_GetAll
List<PersonModel> output;
List<string> mylist;
using (SqlConnection connection = new SqlConnection(GlobalConfig.CnnString(db)))
{
connection.Open();
using(SqlCommand cmd = new SqlCommand("dbo.spPeople_GetAll", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
DataSet dataset = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dataset);
output = (from r in dataset.Tables["dbo.People"].AsEnumerable() select r.Field<int>("id") + r.Field<string>("FirstName") + r.Field<string>("LastName") + r.Field<string>("EmailAddress") + r.Field<string>("CellPhoneNumber")).ToList();
}
}
return output;
}
Here is what the model looks like:
namespace TrackerLibrary.Models
{
public class PersonModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public string CellphoneNumber { get; set; }
public string FullName
{
get
{
return $"{ FirstName } { LastName }";
}
}
}
}
Here is what the stored procedure looks like:
USE [testorama2]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spPeople_GetAll]
AS
BEGIN
SET NOCOUNT ON;
select * from dbo.People
END