Solved

convert sqlreader to datatable

Posted on 2008-10-29
11
886 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

680 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