Matthew
Matthew

Reputation: 4607

C# - Convert SqlDataReader to ArrayList

Is there a way to convert SqlDataReader to arrayList?

I have to pass the results of a query from one page to another so that I can then bind it to a gridview. However, I can't pass the SqlDataReader object in the session.

How can I achieve the following?:

if (rdr.HasRows == true)
                    {
                        while (rdr.Read())
                        {
                             arraylist.add(whole row); //pseudo code
                        }
                    }

Thank you :)

Upvotes: 0

Views: 2377

Answers (1)

Steve
Steve

Reputation: 216293

You could use a SqlDataReader to build a DataTable and pass this object

DataTable dt = new DataTable();
if (rdr.HasRows == true)
     dt.Load(rdr);

A very complete example with various options available could be found on MSDN
Now you could pass the whole DataTable instance and use it to bind the GridView

Upvotes: 3

Related Questions