Link to home
Start Free TrialLog in
Avatar of StewSupport
StewSupport

asked on

convert sqlreader to datatable

i have a function in a class that return rs which is defined as
dim rs as sqldatareader
rs= objcmd.executereader()
return rs

then i call the function in the default.aspx.vb page  and let the table load the rs.
table1.load(rs)

but i can't do it. it keeps saying that table1.load(rs) --expression does not produce value.
could some one helps me figure this out. i want the number of columns and rows to be dynamic. thank you.
Avatar of AmarIs26
AmarIs26

note that the above code is in c# but converting it to vb should be very easy.
Avatar of Kevin Cross
You can use a SqlDataAdapter with the command object instead and then just use the data adapter method .Fill(DataTable) method.

Or you can build your DataTable objects structure and then do it manually like this:

Dim sqlDR As SqlDataReader = GetDataReader(cmd)
           
            While sqlDR.Read()
                'Update data table columns with specific sqlDR("ColumnName") value
            End While

            sqlDR.Close()
Sorry for duplication of what Amarls26 may have said, I had this typed in and got distracted and just hit enter. :)  Hopefully it is still of some use to you.
Avatar of StewSupport

ASKER

i have to use store procedure and then returns the data that is why i use reader then convert to table to put it into a tool that use tool1.datasource = table

otherwise i would have used adapter.
i tried to modified Amarls26  code on that link he gave but it deson't work and it doesnt even see  DataUtils.DataReaderAdapter
I have converted the code to vb.net it should work. I have not tested it but it compiles fine.
you should just be able to do
datatable = DataReaderAdapter . GetDataReader(existingdatareader)
this doesnt work. no such datareaderadapter found
Have you copied the code (DataReaderAdapter class)  i have written and put that in your project?

 
i have dones this before and it worked fine
Dim rs As SqlDataReader = AllFunctions.get_name(Name)
Dim dtreport As Data.DataTable = New Data.DataTable
                dtreport.Load(rs)

but somehow it doesn't work now. lol i got to find out why
ASKER CERTIFIED SOLUTION
Avatar of StewSupport
StewSupport

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice one!  Don't use the DataTable.Load function much, but I should. :) I like using the generic DataTable and DataRow in code and hide the database details in data layer...Glad you found it.

Since DataTable.Load is a sub, I would think having two lines approach was more appropriate.

What you found that works suggested it is acting as though that is a function that returns DataTable or interface accepted by datasource. :) Very interesting.