Solved

convert sqlreader to datatable

Posted on 2008-10-29
11
872 Views
Last Modified: 2011-10-03
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.
0
Comment
Question by:StewSupport
  • 4
  • 4
  • 3
11 Comments
 
LVL 6

Expert Comment

by:AmarIs26
Comment Utility
0
 
LVL 6

Expert Comment

by:AmarIs26
Comment Utility
note that the above code is in c# but converting it to vb should be very easy.
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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()
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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.
0
 

Author Comment

by:StewSupport
Comment Utility
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:AmarIs26
Comment Utility
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)
0
 

Author Comment

by:StewSupport
Comment Utility
this doesnt work. no such datareaderadapter found
0
 
LVL 6

Expert Comment

by:AmarIs26
Comment Utility
Have you copied the code (DataReaderAdapter class)  i have written and put that in your project?

 
0
 

Author Comment

by:StewSupport
Comment Utility
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
0
 

Accepted Solution

by:
StewSupport earned 0 total points
Comment Utility
omg i'm so stupid.
i put these two lines together and make it not work.
gridview.datasource = dt.load(rs)
instead of
dt.load(rs)
gridview.datasource = dt
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now