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.

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

note that the above code is in c# but converting it to vb should be very easy.
Kevin CrossChief Technology OfficerCommented:
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

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Kevin CrossChief Technology OfficerCommented:
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.
StewSupportAuthor Commented:
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 it should work. I have not tested it but it compiles fine.
you should just be able to do
datatable = DataReaderAdapter . GetDataReader(existingdatareader)
StewSupportAuthor Commented:
this doesnt work. no such datareaderadapter found
Have you copied the code (DataReaderAdapter class)  i have written and put that in your project?

StewSupportAuthor Commented:
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

but somehow it doesn't work now. lol i got to find out why
StewSupportAuthor Commented:
omg i'm so stupid.
i put these two lines together and make it not work.
gridview.datasource = dt.load(rs)
instead of
gridview.datasource = dt

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kevin CrossChief Technology OfficerCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.