Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i fill list<string> from database just like fill datatable with the help of data adapter?
i want to optimize my page and i want to replace the datatable with List. how do i archive this?
i want to use List as dataSource of the control in asp.net.
Posted
Updated 5-Mar-14 8:36am
v2

Let's say,
C#
class Student
{
    public int ID
    { get; set; }
    
    public string Name
    { get; set; }
}

Now you've got DataTable, iterate through all records like below and store it in a list,
C#
List<Student> stud = new List<Student>();
foreach(DataRow dr in dt.Rows)  // dt is a DataTable
{
    stud.Add(new Student{ ID = int.Parse(dr["Id"].ToString()), Name = dr["Name"].ToString()});
}

return stud;

-KR
 
Share this answer
 
v3
Comments
utm 5-Mar-14 23:46pm    
thanks for reply..
i got your point and its good but some Question arises..
i don't want to use datatable. if i use datatable then why to use List write some extra code for fill List,it doesn't make sense and all work can be done by datatable then why List?
and
according to above code ... do i have to create class with properties whenever i have need different type of results return from databse?
Krunal Rohit 6-Mar-14 2:01am    
As you have used SqlDataAdapter, you must have used DataTable or DataSet.
And it's not necessary that you should create class, you can store the data multiple strings as well.

-KR
utm 6-Mar-14 4:20am    
no i am not using sqldataadapter .. i am asking for alternative way without datatable and sqldataadapter filling the List ..
my code is here..

SqlConnection connectToDb = new SqlConnection(mycon);
DataTable dt = new DataTable(); // not want to use
SqlCommand cmd = new SqlCommand(getsp, connectToDb);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd); //not want to use
da.Fill(dt); // not want to use
return dt; // not want to use
***********************************************************************
list<string> lst = new List<string>

SqlConnection connectToDb = new SqlConnection(mycon);
SqlCommand cmd = new SqlCommand(getsp, connectToDb);
cmd.CommandType = CommandType.StoredProcedure;

how to fill list with data?

return lst


hope this time u understand my requirement.
utm 7-Mar-14 1:38am    
hello rohit ..is there any problem or my Question is not understandable.
if any problem plz tell..
Data adapters work with Data tables and Data sets only. If you want to use your own classes with generic collections have a look on a proper ORM like Entity framework for example. There are plenty of articles around here to get you started:
Introduction to Entity Framework
An Introduction to Entity Framework for Absolute Beginners
 
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