473,714 Members | 2,500 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SqlDataReader & Stored Procedure

Suppose a SQL Server 2005 stored procedure looks like this:

ALTER PROCEDURE SPName
@UserID int

SELECT COUNT(*) FROM Table1 WHERE UserID = @UserID
SELECT COUNT(*) FROM Table1

In the ASPX page, I can get the result of the first query in the above
SP using

While (sqlReader.Read er)
Response.Write( sqlReader.GetVa lue(0)
End While

But how do I get the result of the second query in the ASPX page?

Oct 19 '06 #1
6 6394
You advance to the next query in the reader. But, note, that this can only
be done after you are finished with the first result set. If you want to
simply chain the two answers, you can use a temp table approach.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************** *************** *************** ****
Think outside of the box!
*************** *************** *************** ****
<rn**@rediffmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Suppose a SQL Server 2005 stored procedure looks like this:

ALTER PROCEDURE SPName
@UserID int

SELECT COUNT(*) FROM Table1 WHERE UserID = @UserID
SELECT COUNT(*) FROM Table1

In the ASPX page, I can get the result of the first query in the above
SP using

While (sqlReader.Read er)
Response.Write( sqlReader.GetVa lue(0)
End While

But how do I get the result of the second query in the ASPX page?

Oct 19 '06 #2
I know it's early, but you didn't say how :P

dr.NextResult()

and as Greg pointed out, you need to be done going through the first one.

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Cowboy (Gregory A. Beamer)" <No************ @comcast.netNoS pamMwrote in
message news:en******** ******@TK2MSFTN GP02.phx.gbl...
You advance to the next query in the reader. But, note, that this can only
be done after you are finished with the first result set. If you want to
simply chain the two answers, you can use a temp table approach.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************** *************** *************** ****
Think outside of the box!
*************** *************** *************** ****
<rn**@rediffmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
>Suppose a SQL Server 2005 stored procedure looks like this:

ALTER PROCEDURE SPName
@UserID int

SELECT COUNT(*) FROM Table1 WHERE UserID = @UserID
SELECT COUNT(*) FROM Table1

In the ASPX page, I can get the result of the first query in the above
SP using

While (sqlReader.Read er)
Response.Write( sqlReader.GetVa lue(0)
End While

But how do I get the result of the second query in the ASPX page?


Oct 19 '06 #3
That's exactly where I am getting stuck. How do I find out that the
first query has been finished with & that I can now move on to the
second query? Using

SqlDataReader1. NextResult

as Karl pointed out?
Cowboy (Gregory A. Beamer) wrote:
You advance to the next query in the reader. But, note, that this can only
be done after you are finished with the first result set. If you want to
simply chain the two answers, you can use a temp table approach.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************** *************** *************** ****
Think outside of the box!
*************** *************** *************** ****
<rn**@rediffmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Suppose a SQL Server 2005 stored procedure looks like this:

ALTER PROCEDURE SPName
@UserID int

SELECT COUNT(*) FROM Table1 WHERE UserID = @UserID
SELECT COUNT(*) FROM Table1

In the ASPX page, I can get the result of the first query in the above
SP using

While (sqlReader.Read er)
Response.Write( sqlReader.GetVa lue(0)
End While

But how do I get the result of the second query in the ASPX page?
Oct 19 '06 #4
How critical is it to you to use a SQL DataReader ?
I mean, the problem would not arise if you are catching the entire result
into a DataSet.
Would'nt a simple objDs.Tables[0] and objDs.Tables[1] for the two different
queries solve the problem?

Just a thought!
Kuldeep

<rn**@rediffmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Suppose a SQL Server 2005 stored procedure looks like this:

ALTER PROCEDURE SPName
@UserID int

SELECT COUNT(*) FROM Table1 WHERE UserID = @UserID
SELECT COUNT(*) FROM Table1

In the ASPX page, I can get the result of the first query in the above
SP using

While (sqlReader.Read er)
Response.Write( sqlReader.GetVa lue(0)
End While

But how do I get the result of the second query in the ASPX page?

Oct 19 '06 #5
while (dr.Read())
{
//do stuff
}

dr.NextResult() ;

while (dr.Read())
{
}

--
http://www.openmymind.net/
http://www.fuelindustries.com/
<rn**@rediffmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
That's exactly where I am getting stuck. How do I find out that the
first query has been finished with & that I can now move on to the
second query? Using

SqlDataReader1. NextResult

as Karl pointed out?
Cowboy (Gregory A. Beamer) wrote:
>You advance to the next query in the reader. But, note, that this can
only
be done after you are finished with the first result set. If you want to
simply chain the two answers, you can use a temp table approach.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

************** *************** *************** *****
Think outside of the box!
************** *************** *************** *****
<rn**@rediffma il.comwrote in message
news:11******* *************** @b28g2000cwb.go oglegroups.com. ..
Suppose a SQL Server 2005 stored procedure looks like this:

ALTER PROCEDURE SPName
@UserID int

SELECT COUNT(*) FROM Table1 WHERE UserID = @UserID
SELECT COUNT(*) FROM Table1

In the ASPX page, I can get the result of the first query in the above
SP using

While (sqlReader.Read er)
Response.Write( sqlReader.GetVa lue(0)
End While

But how do I get the result of the second query in the ASPX page?

Oct 19 '06 #6
This is what I finally did & it works fine:

Stored Procedure:
----------------

ALTER PROCEDURE dbo.SPName
@UserID int
AS
DECLARE
@count int

SELECT COUNT(*) AS TotalCount FROM Table1
SET @count = (SELECT COUNT(*) FROM Table1 WHERE UserID = @UserID)
RETURN @count

ASPX Code:
---------

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim sqlCmd As SqlCommand
Dim sqlConn As SqlConnection
Dim sqlReader As SqlDataReader

sqlConn = New SqlConnection(" ......")
sqlCmd = New SqlCommand("SPN ame", sqlConn)
sqlCmd.CommandT ype = CommandType.Sto redProcedure

With sqlCmd
.Parameters.Add ("@ReturnValue" , SqlDbType.Int, 4).Direction
= ParameterDirect ion.ReturnValue
.Parameters.Add ("@UserID", SqlDbType.Int). Value =
CInt(Request.Qu eryString("UID" ))
End With

sqlConn.Open()
sqlReader = sqlCmd.ExecuteR eader

While (sqlReader.Read )
lblOutput.Text = "Total Count: " & sqlReader.GetIn t32(0) &
"<br>"
End While
sqlReader.Close ()

sqlCmd.ExecuteN onQuery()
lblOutput.Text += "Count: " & sqlCmd.Paramete rs(0).Value

sqlConn.Close()
End Sub
</script>
<form runat="server">
<asp:Label ID="lblOutput" runat="server"/>
</form>

I will definitely try it out using your approach, Karl.

Karl Seguin [MVP] wrote:
while (dr.Read())
{
//do stuff
}

dr.NextResult() ;

while (dr.Read())
{
}

--
http://www.openmymind.net/
http://www.fuelindustries.com/
<rn**@rediffmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
That's exactly where I am getting stuck. How do I find out that the
first query has been finished with & that I can now move on to the
second query? Using

SqlDataReader1. NextResult

as Karl pointed out?
Cowboy (Gregory A. Beamer) wrote:
You advance to the next query in the reader. But, note, that this can
only
be done after you are finished with the first result set. If you want to
simply chain the two answers, you can use a temp table approach.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************** *************** *************** ****
Think outside of the box!
*************** *************** *************** ****
<rn**@rediffmai l.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Suppose a SQL Server 2005 stored procedure looks like this:

ALTER PROCEDURE SPName
@UserID int

SELECT COUNT(*) FROM Table1 WHERE UserID = @UserID
SELECT COUNT(*) FROM Table1

In the ASPX page, I can get the result of the first query in the above
SP using

While (sqlReader.Read er)
Response.Write( sqlReader.GetVa lue(0)
End While

But how do I get the result of the second query in the ASPX page?
Oct 19 '06 #7

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

Similar topics

1
2698
by: Neo Chou | last post by:
Greetings! I met the same question as in ADO a few months ago. I'm working on MS SQL Server 2000. I have a stored procedure that returns a return value as well as a record set (by "select" statement). Below is my ASP code: <% Set OBJdbConn = Server.CreateObject("ADODB.Connection")
2
1540
by: Binny | last post by:
I have a stored procedure which returns a recordset and also returns a value. I know how to access the recordset. How can i access the integer value returned by the stored procedure Iam using SqlCommand.ExecuteReader thanks
2
2930
by: Cameron Frasnelly | last post by:
I emulated the code from the .Net Framework help (Titled "Using Stored Procedures with a Command") and I still receive and error... Error Received = "Invalid attempt to read when no data is present." BUT when I manually run the Stored Procedure and manually type in '3333' it works just fine by returning the single row for that particular work order???????? *****More Clues****
5
2156
by: James Wong | last post by:
Hi, I am writing a vb.net2005 program that needs to create a stored procedure with SqlServerProject Template. Now, I have two questions for this stored procedure. 1) How can I import and execute the .dll in this Stored Procedures? 2) How can I connect the Web Service and get the result in this Stored Procedures?
6
3057
by: Varangian | last post by:
I have an SQL Server 2005 Express Stored Procedure .. which first I select if that record exists, if it exists I update that record .. if not I insert. It then selects the record with the new data updated or inserted... however the .NET SqlDataReader always takes the first query results (which the query that first ran to check if that record exists) .. How can I take the second query results?. I can use the Try Catch but that will...
2
5377
by: rn5a | last post by:
The following code calls a function in a DLL which returns a SqlDataReader (the function in the DLL calls a stored procedure which returns 6 columns): Dim boCart As Cart Dim sqlReader As SqlDataReader boCart = New Cart boCart.GetRecords(iUserID)
4
4360
by: scparker | last post by:
Hello, We have a stored procedure that does a basic insert of values. I am then able to retrieve the ID number created for this new record. We are currently using ASP.NET 2.0 and use N-Tier Architecture. The Stored Procedures are used through TableAdaptors, which in turn are used by Class Files. I wish to be able to return this new ID value using the Stored
3
2745
by: Sam | last post by:
Hi All, I have a very strange issue with ms sql stored procedure and sqlDataReader and I hope that someone can tell me what's going on . I have an asp.net application which has one of its page populated with data onto a datagrid and recently the records are not sorted correctly on the datagrid(we do all the sorting in our stored procedure and no sorting on the code behind page). The strange things is that:
4
3664
by: Cirene | last post by:
I have a sqldatareader that I use to read some data. Later I do a dr.close. In the same sub I later to "dr = MyCommand.ExecuteReader" because I'm trying to reuse the var with a totally different stored proc. When the code runs I get this error on the 2nd dr.Read: System.InvalidOperationException: Invalid attempt to call Read when reader is closed. at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout) at...
0
8801
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
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
9015
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...
1
6634
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
5947
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4464
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...
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2110
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.