Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have a stored procedure which gets a table, Please tell how can i get all the values to a list.
Please help with a sample code, if possible
Posted

1 solution

Hello,

Suppose following is your DataTable which return Stored Procedure..
DataTable dtExample = new DataTable("Example");
dtExample.Columns.Add("EmpID");
dtExample.Columns.Add("Name");


If You have following Class like
C#
public class Employees
{
        public String EmpID { get ;  set ;}
        public String Name{ get ;  set ;}
}



Create function that will be return Employee Class from DataRow
C#
private Employees GetEmpDataTableRow(DataRow dr)
{
     Employees oEmp = new Employees();
     oEmp.EmpID = dr["EmpID"].ToString();
     oEmp.Name = dr["Name"].ToString(); 
     return oEmp ;
}


Finnally call it using Loop like

//Create List of Employees Class
List<employees>  objEmpList=new List<employees>();

//dtExample = Your Stored Procedure Result
  foreach (DataRow row in dtExample.Rows)
           objEmpList.Add(row);


//Finnaly you get List in objEmpList

</employees></employees>
 
Share this answer
 

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