Possible Duplicate:
How can I easily convert DataReader to List<T>?
I want to put the data which is coming from datareader object in a generic list. I have done like this but it doesn’t work…
I am getting cast exception error in foreach row!
SqlDataReader pointreader = cmd2.ExecuteReader();
var pointt = new List<int>();
while (pointreader.Read())
{
foreach (int item in pointreader)
{
pointt.Add(item);
if (pointt.Contains(point))
{
matchpoint = item;
}
}
}
SqlDataReader cannot be accessed in the way you have described as it does not implement IEnumerable. Instead you need to access each field individually:
If you’ve only selected one column and are certain that it is an int then you can simplify the code like so:
This article may be of some use.