Solved

convert sqlreader to datatable

Posted on 2008-10-29
11
879 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
ID: 22831316
0
 
LVL 6

Expert Comment

by:AmarIs26
ID: 22831325
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
ID: 22831554
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22831566
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
ID: 22831635
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
 
LVL 6

Expert Comment

by:AmarIs26
ID: 22831834
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
ID: 22831969
this doesnt work. no such datareaderadapter found
0
 
LVL 6

Expert Comment

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

 
0
 

Author Comment

by:StewSupport
ID: 22832061
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
ID: 22832113
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
ID: 22833428
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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