0

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'.
1
  • I don't get it. What are you trying to do? Put strings from all available columns into a list, regardless of the column name or type? Commented Dec 17, 2011 at 12:27

1 Answer 1

1

try this:

while (myReader.Read())
{
  list.Add(myReader.GetString(0));
}
Sign up to request clarification or add additional context in comments.

4 Comments

I am not sure why I got this error: Cannot convert type 'char' to 'string'
edit your question with your new code, without seeing it is impossible to answer!
This answer is apparently wrong. GetString returns a string, and foreach in a string is looping through the characters of the string.
If you donnt know the datatype , you could use myReader.GetValue(0) + ""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.