views:

60

answers:

1

Using VB9, I'm trying to populate a DataTable with the results from several stored procedure calls. What I'm trying to make work looks like:

For Each record In New SqlCommand("exec getResults", conn).ExecuteReader
    Dim dr As DataRow = dt.NewRow
    record.GetValues(dr.ItemArray)
    dt.Rows.Add(dr)
Next

It seems like the DataRow.ItemArray and DbDataRecord.GetValues ought to plug together neatly -- and it does compile... But when it runs, the DataRow is filled with null values instead of the results from the SqlDataReader.

I can loop across the fields in the DbDataRecord and insert them one by one into the DataRow like:

For Each record In New SqlCommand("exec getResults", conn).ExecuteReader
    Dim dr As DataRow = dt.NewRow
    For fieldLoop = 0 To 9
        dr.Item(fieldLoop) = record(fieldLoop)
    Next
    dt.Rows.Add(dr)
Next

but doesn't seem like that should be needed. I'm not finding MSDN to be helpful on this one.

So, am I barking up the wrong tree entirely? Or if I just have a little error, what is it?

+1  A: 

use the datatable.load method and pass in the datareader object. http://msdn.microsoft.com/en-us/library/system.data.datatable.load.aspx

bugtussle
Wow, that's easy. :)
clweeks