tags:

views:

32

answers:

2

Hi..

I want to return list which will store the sqldatareader value. how can i do this..

+1  A: 
using (var reader = cmd.ExecuteReader())
{
    var result = new List<Foo>();
    while (reader.Read())
    {
        var foo = new Foo 
        {
            Prop1 = reader.GetInt32(0),
            Prop2 = reader.GetString(1),
        }
        result.Add(foo);
    }
}
Darin Dimitrov
A: 

Also check this previously asked qestion :

http://stackoverflow.com/questions/1464883/how-can-i-easily-convert-datareader-to-listt

Check this for generic type :

public ConvertToList<T>(SqlDataReader sqldr, int index)
        {
            List<T> list = new List<T>();

            while (sqldr.Read())           {
               list.Add((T)sqldr.GetValue(index));               index++;   
            }

            return list;
        }
Pranay Rana