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'.
有帮助吗?

解决方案

try this:

while (myReader.Read())
{
  list.Add(myReader.GetString(0));
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top