OleDbDataReader to ArrayList in C#
-
19-03-2021 - |
Question
How to iterate through OleDbDataReader
and put its elements into ArrayList
?
Here is my code:
// ...
ArrayList list = new ArrayList();
while(myReader.Read())
{
foreach(string s in myReader) // I got an Exception here
{
list.Add(s);
}
}
// ...
Label lbl = new Label();
lbl.Text = list[i] as string;
and here is the Exception:
System.InvalidCastException: Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.String'.
Solution
try this:
while (myReader.Read())
{
list.Add(myReader.GetString(0));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow