473,614 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning multiple resultsets usind datadreader

I'm using this coding to get 2 resultsets thru datareader and then load them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextRes ult() is returning false for some reason. Is that possible
the connection is closed?

Dim strConn As String = "Server=localho st;Database=nor thwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(s trConn)
Try
Dim strSql As String = "select * from customers;selec t * from
products"
Dim cmdGrid As New SqlCommand(strS ql, cnnNwind)
cmdGrid.Command Type = CommandType.Tex t
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.Execute Reader
Dim dt1 As New DataTable
dt1.Load(sdrGri d)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Mess age)
Finally
cnnNwind.Close( )
End Try
Aug 23 '07 #1
10 2077
Peter,

Can you explain me why you use this method, as far as I can see it now, it
is going from NY to Wasinton to Jersey City over LA.

Normally we use direct the DataAdapter Fill, however there should be a
reason?
It cannot be that it is quicker, because that is AFAIK culprit, you have to
write at least more code while the result is the same, although you need
probably after what you do now at least an acceptchanges of course, which is
automaticly done by the fill.

However, there must be a reason and I am currious about that?

Cor

"Peter" <Pe***@discussi ons.microsoft.c omschreef in bericht
news:DF******** *************** ***********@mic rosoft.com...
I'm using this coding to get 2 resultsets thru datareader and then load
them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextRes ult() is returning false for some reason. Is that
possible
the connection is closed?

Dim strConn As String = "Server=localho st;Database=nor thwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(s trConn)
Try
Dim strSql As String = "select * from customers;selec t * from
products"
Dim cmdGrid As New SqlCommand(strS ql, cnnNwind)
cmdGrid.Command Type = CommandType.Tex t
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.Execute Reader
Dim dt1 As New DataTable
dt1.Load(sdrGri d)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Mess age)
Finally
cnnNwind.Close( )
End Try
Aug 23 '07 #2
Hi Cor,

I'm just trying different ways to retrieve data: datareader, dataadapter,
tableadapter, DAAB , and LINQ.

One of the objectives is to retrieve master-details resultsets and populate
them into datagridviews for read-only purpose. This probably will be
implemented as a class for generic usage.

Peter

"Cor Ligthert[MVP]" wrote:
Peter,

Can you explain me why you use this method, as far as I can see it now, it
is going from NY to Wasinton to Jersey City over LA.

Normally we use direct the DataAdapter Fill, however there should be a
reason?
It cannot be that it is quicker, because that is AFAIK culprit, you have to
write at least more code while the result is the same, although you need
probably after what you do now at least an acceptchanges of course, which is
automaticly done by the fill.

However, there must be a reason and I am currious about that?

Cor

"Peter" <Pe***@discussi ons.microsoft.c omschreef in bericht
news:DF******** *************** ***********@mic rosoft.com...
I'm using this coding to get 2 resultsets thru datareader and then load
them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextRes ult() is returning false for some reason. Is that
possible
the connection is closed?

Dim strConn As String = "Server=localho st;Database=nor thwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(s trConn)
Try
Dim strSql As String = "select * from customers;selec t * from
products"
Dim cmdGrid As New SqlCommand(strS ql, cnnNwind)
cmdGrid.Command Type = CommandType.Tex t
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.Execute Reader
Dim dt1 As New DataTable
dt1.Load(sdrGri d)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Mess age)
Finally
cnnNwind.Close( )
End Try
Aug 23 '07 #3
Hello Peter ,

did you set the MARS parameter to true in the connection string ?

Dim objConn As New SqlConnection(" MultipleActiveR esultSets=True; Persist
Security Info=False;User ID=sa;Password= pwd;Initial Catalog=tempdat abase")
"Peter" <Pe***@discussi ons.microsoft.c omschreef in bericht
news:DF******** *************** ***********@mic rosoft.com...
I'm using this coding to get 2 resultsets thru datareader and then load
them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextRes ult() is returning false for some reason. Is that
possible
the connection is closed?

Dim strConn As String = "Server=localho st;Database=nor thwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(s trConn)
Try
Dim strSql As String = "select * from customers;selec t * from
products"
Dim cmdGrid As New SqlCommand(strS ql, cnnNwind)
cmdGrid.Command Type = CommandType.Tex t
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.Execute Reader
Dim dt1 As New DataTable
dt1.Load(sdrGri d)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Mess age)
Finally
cnnNwind.Close( )
End Try

Aug 23 '07 #4
Hi Michel,

I guess this may be the problem. Will need to try it tomorrow.

Thanks,

Peter

"Michel Posseth [MCP]" wrote:
Hello Peter ,

did you set the MARS parameter to true in the connection string ?

Dim objConn As New SqlConnection(" MultipleActiveR esultSets=True; Persist
Security Info=False;User ID=sa;Password= pwd;Initial Catalog=tempdat abase")
"Peter" <Pe***@discussi ons.microsoft.c omschreef in bericht
news:DF******** *************** ***********@mic rosoft.com...
I'm using this coding to get 2 resultsets thru datareader and then load
them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextRes ult() is returning false for some reason. Is that
possible
the connection is closed?

Dim strConn As String = "Server=localho st;Database=nor thwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(s trConn)
Try
Dim strSql As String = "select * from customers;selec t * from
products"
Dim cmdGrid As New SqlCommand(strS ql, cnnNwind)
cmdGrid.Command Type = CommandType.Tex t
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.Execute Reader
Dim dt1 As New DataTable
dt1.Load(sdrGri d)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Mess age)
Finally
cnnNwind.Close( )
End Try


Aug 23 '07 #5
Peter,

If you change this:

If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If

to this:

dt2.Load(sdrGri d)
DGVC.DataSource = dt2

then I think it will work.

Kerry Moorman

"Peter" wrote:
I'm using this coding to get 2 resultsets thru datareader and then load them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextRes ult() is returning false for some reason. Is that possible
the connection is closed?

Dim strConn As String = "Server=localho st;Database=nor thwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(s trConn)
Try
Dim strSql As String = "select * from customers;selec t * from
products"
Dim cmdGrid As New SqlCommand(strS ql, cnnNwind)
cmdGrid.Command Type = CommandType.Tex t
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.Execute Reader
Dim dt1 As New DataTable
dt1.Load(sdrGri d)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Mess age)
Finally
cnnNwind.Close( )
End Try
Aug 23 '07 #6
Hi Michel,

I have tried setting this to true or false and has no effect. I'm
connecting to SQL Server 2005 and I believe MARS is true by default.

The coding suddenly works after I removed the If statement from the
following coding:

Original coding:
If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If

Revised coding:
dt2.Load(sdrGri d)
DGVC.DataSource = dt2

This is kind of confusing since I think I'm getting multiple resultsets and
need to the NextResult method to move to next resultset. With the revised
coding, I have tried both MARS=true and MARS=fales and it works on both
settings.
Peter

"Michel Posseth [MCP]" wrote:
Hello Peter ,

did you set the MARS parameter to true in the connection string ?

Dim objConn As New SqlConnection(" MultipleActiveR esultSets=True; Persist
Security Info=False;User ID=sa;Password= pwd;Initial Catalog=tempdat abase")
"Peter" <Pe***@discussi ons.microsoft.c omschreef in bericht
news:DF******** *************** ***********@mic rosoft.com...
I'm using this coding to get 2 resultsets thru datareader and then load
them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextRes ult() is returning false for some reason. Is that
possible
the connection is closed?

Dim strConn As String = "Server=localho st;Database=nor thwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(s trConn)
Try
Dim strSql As String = "select * from customers;selec t * from
products"
Dim cmdGrid As New SqlCommand(strS ql, cnnNwind)
cmdGrid.Command Type = CommandType.Tex t
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.Execute Reader
Dim dt1 As New DataTable
dt1.Load(sdrGri d)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Mess age)
Finally
cnnNwind.Close( )
End Try


Aug 23 '07 #7
Hi Kerry,

It works but I have no idea why. I believe my coding was originally
without the If sdrGrid.NextRes ult() = True and it didn't work. So, I tried
the NextResult coding but still not working.
Peter

"Kerry Moorman" wrote:
Peter,

If you change this:

If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If

to this:

dt2.Load(sdrGri d)
DGVC.DataSource = dt2

then I think it will work.

Kerry Moorman

"Peter" wrote:
I'm using this coding to get 2 resultsets thru datareader and then load them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextRes ult() is returning false for some reason. Is that possible
the connection is closed?

Dim strConn As String = "Server=localho st;Database=nor thwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(s trConn)
Try
Dim strSql As String = "select * from customers;selec t * from
products"
Dim cmdGrid As New SqlCommand(strS ql, cnnNwind)
cmdGrid.Command Type = CommandType.Tex t
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.Execute Reader
Dim dt1 As New DataTable
dt1.Load(sdrGri d)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Mess age)
Finally
cnnNwind.Close( )
End Try
Aug 23 '07 #8
Hi Michel,

I think my case is not multiple ACTIVE resultsets. My case is simply a
batch which return multiple resultsets. I have not found any articles
explaining the difference between them. But the MARS parameter will only
have effect if I try to use 2 datareaders without closing the 1st datareader.
Peter

"Michel Posseth [MCP]" wrote:
Hello Peter ,

did you set the MARS parameter to true in the connection string ?

Dim objConn As New SqlConnection(" MultipleActiveR esultSets=True; Persist
Security Info=False;User ID=sa;Password= pwd;Initial Catalog=tempdat abase")
"Peter" <Pe***@discussi ons.microsoft.c omschreef in bericht
news:DF******** *************** ***********@mic rosoft.com...
I'm using this coding to get 2 resultsets thru datareader and then load
them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextRes ult() is returning false for some reason. Is that
possible
the connection is closed?

Dim strConn As String = "Server=localho st;Database=nor thwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(s trConn)
Try
Dim strSql As String = "select * from customers;selec t * from
products"
Dim cmdGrid As New SqlCommand(strS ql, cnnNwind)
cmdGrid.Command Type = CommandType.Tex t
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.Execute Reader
Dim dt1 As New DataTable
dt1.Load(sdrGri d)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Mess age)
Finally
cnnNwind.Close( )
End Try


Aug 23 '07 #9
Hi Kerry,

I think I find out the reason why NextResult is not needed. The
datatable.load will position to the next resultset automatically.
Peter

"Kerry Moorman" wrote:
Peter,

If you change this:

If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If

to this:

dt2.Load(sdrGri d)
DGVC.DataSource = dt2

then I think it will work.

Kerry Moorman

"Peter" wrote:
I'm using this coding to get 2 resultsets thru datareader and then load them
into 2 datatables and bind the datatables to datagridviews. But
sdrGrid.NextRes ult() is returning false for some reason. Is that possible
the connection is closed?

Dim strConn As String = "Server=localho st;Database=nor thwind;" + _
"Integrated Security=SSPI"
Dim cnnNwind As SqlConnection = New SqlConnection(s trConn)
Try
Dim strSql As String = "select * from customers;selec t * from
products"
Dim cmdGrid As New SqlCommand(strS ql, cnnNwind)
cmdGrid.Command Type = CommandType.Tex t
cnnNwind.Open()
Dim sdrGrid As SqlDataReader = cmdGrid.Execute Reader
Dim dt1 As New DataTable
dt1.Load(sdrGri d)
DGVP.DataSource = dt1
Dim dt2 As New DataTable
If sdrGrid.NextRes ult() = True Then
dt2.Load(sdrGri d)
DGVC.DataSource = dt2
End If
Catch exc As Exception
MsgBox(exc.Mess age)
Finally
cnnNwind.Close( )
End Try
Aug 23 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5313
by: Eli Sidwell | last post by:
Trying to return a Recordset to an ASP and the Recordset is empty. The StorredProc works in the query analyzer and it even works from a quick VB app that I wrote to test it. The storedproc that I am using is fairly complex (creates some temporary tables and populates them with 'Insert Into Select ...', but the during testing the only Select statements that return visible rows is the final one that returns the finished table with an...
1
2589
by: Top Gun | last post by:
In order to avoid multiple trips to the database, I would like to fill several tables in a DataSet with a single call to a stored procedure that will return resultsets for the appropriate tables. Can the SqlDataReader be used for this?
1
2608
by: Frank Rizzo | last post by:
I have a stored proc that returns 5 resultsets. How can I read each resultset into a separate data reader? Thanks
4
2629
by: Amy | last post by:
Hello, I've been struggling to learn C#.NET for a while now. I've made some progress, but I'm easily stumped. :( What's stumping me today is this: I've got a stored procedure (SQL) that returns one row from a table, and up to 3 rows from another table. I want to read the values of the three rows into an array, but I can't figure out how to do it. The code I have compiles, but I suspect it's not quite what I'm looking
5
25483
by: praveen | last post by:
Hi, I am using nested resultsets to execute queries on two different tables. The code structure is: stmt1 = conn.createStatement(); rs1 = stmt1.executeQuery(query1); while (rs1.next()) { ..... stmt2 = conn.createStatement();
1
2065
by: Robson Siqueira | last post by:
Folks, For designing WinApps, I do prefer to have the controls dragged and dropped into the screen, mainly for datagridview controls. For that end, I normally use the DataSet designer but reading the dataset information from my tables/views/stored procedures. Now I have a case in which I want to use the designer, but with a procedure returning multiple resultsets. I found a lot of articles on the internet, but they say everything but...
6
2141
by: iKiLL | last post by:
Hi all I am developing in C#, CF2 and SQL Mobile. Currently my app is using Merge Replication. This is all working well. I have now decided to try and use Result sets in my application but I am not sure on how to handle this. I have gone through examples on how to build a result set, but my
4
9119
by: Chris | last post by:
This might be a stupid question.... I have a stored procedures, which uses two selects. When I run the SP I get two resultsets, one very big and the other much smaller, which is the one I want in the dataset. How do a ensure only the last one goes in the dataset.
0
1923
by: Camfa | last post by:
Hi all, I’ve been messing around with the following question for 2 days now, and i still haven’t find a fix. So i hope you can help me. I’ll quickly show you guys the situation: There are 2 tables in my database (MainRecords and RecordLines) http://img502.imageshack.us/img502/6106/startscreencv5.png
0
8640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8589
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8287
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6093
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4136
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.