Datatable to List in C# using Linq
//StudentDetails Class with Entity Details
public class StudentDetails
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public string ClassName { get; set; }
public string Section { get; set; }
}
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public string ClassName { get; set; }
public string Section { get; set; }
}
//Dataaccess part, Microsoft Sqlhelper class used
public List GetStudentDetails(int studentID)
{
DataSet ds = new DataSet();
SqlParameter[] SqlParam = null;
SqlParam = new SqlParameter[1];
SqlCommand SqlCmd = new SqlCommand();
List objStud =
new List();
try
{
SqlParam[0] = new SqlParameter("@StudentID", SqlDbType.Int);
SqlParam[0].Value = studentID;
SqlHelper.FillDataset(DBconnection(), CommandType.StoredProcedure, "usp_Get_StudentDetails", ds, new string[] { "Display" }, SqlParam);
objStud =
ds.Tables[0].AsEnumerable().Select(data => new StudentDetails() { StudentID
= (int)data["StudentID"], StudentName =
(string)data["StudentName"], ClassName =
(string)data["ClassName"], Section =
(string)data["Section"] }).ToList();
return objStud;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (ds != null)
{
ds.Dispose();
}
objStud = null;
}
}
{
DataSet ds = new DataSet();
SqlParameter[] SqlParam = null;
SqlParam = new SqlParameter[1];
SqlCommand SqlCmd = new SqlCommand();
List
try
{
SqlParam[0] = new SqlParameter("@StudentID", SqlDbType.Int);
SqlParam[0].Value = studentID;
SqlHelper.FillDataset(DBconnection(), CommandType.StoredProcedure, "usp_Get_StudentDetails", ds, new string[] { "Display" }, SqlParam);
return objStud;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (ds != null)
{
ds.Dispose();
}
objStud = null;
}
}
No comments:
Post a Comment
Comments Welcome