473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.Reader)
Response.Write(sqlReader.GetValue(0)
End While

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

Oct 19 '06 #1
6 6380
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**@rediffmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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.Reader)
Response.Write(sqlReader.GetValue(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.netNoSpamMwrote in
message news:en**************@TK2MSFTNGP02.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**@rediffmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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.Reader)
Response.Write(sqlReader.GetValue(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**@rediffmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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.Reader)
Response.Write(sqlReader.GetValue(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**@rediffmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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.Reader)
Response.Write(sqlReader.GetValue(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**@rediffmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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**@rediffmail.comwrote in message
news:11**********************@b28g2000cwb.googleg roups.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.Reader)
Response.Write(sqlReader.GetValue(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("SPName", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure

With sqlCmd
.Parameters.Add("@ReturnValue", SqlDbType.Int, 4).Direction
= ParameterDirection.ReturnValue
.Parameters.Add("@UserID", SqlDbType.Int).Value =
CInt(Request.QueryString("UID"))
End With

sqlConn.Open()
sqlReader = sqlCmd.ExecuteReader

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

sqlCmd.ExecuteNonQuery()
lblOutput.Text += "Count: " & sqlCmd.Parameters(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**@rediffmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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**@rediffmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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.Reader)
Response.Write(sqlReader.GetValue(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
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"...
2
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...
2
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...
5
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...
6
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...
2
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...
4
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...
3
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.