473,404 Members | 2,179 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,404 software developers and data experts.

returning output vars from nested stored procedure

Hi all,

I have an app that currently runs through 3 seperate stored procedures each
returning a count of records. What I would like to do is combine these
calls into one call, however I am having an issue getting the output
parameters' values after execution. Here is a snipit of code that calls one
for simplicity's sake: I think I am close, i was under the impression I
could use the datareader to get at it, but is is always returning 0 :)

Cheers & thanks a bunch, Joe :)
'SQL

'TOP LEVEL CALLING SP
CREATE PROCEDURE [dbo].[desktopquery]

@empid Int,
@deptid Int

AS

EXEC empprojectcnt @empid Int, @projectcnt Int Output
GO

'PROJECTS
CREATE PROCEDURE [dbo].[empprojectcnt]

@empid Int,
@projectcnt Int Output

AS

Select @projectcnt = COUNT(projectid) FROM PROJECT WHERE empid = @empid
Return @projectcnt
GO

'ASP NET

Private Sub GetEmpStats()

Dim curEmpID As Integer = CInt(Session("empid"))

Dim curDeptID As Integer = CInt(Session("deptid"))

Dim MyConn As New
SqlConnection(System.Configuration.ConfigurationSe ttings.AppSettings("dbConn
"))

Dim MySQLe As String = "[dbo].[desktopquery]"

Dim Cmda As New SqlCommand(MySQLe, MyConn)

Cmda.CommandType = CommandType.StoredProcedure

Dim objDR As SqlDataReader

'INPUT parameters

Cmda.Parameters.Add(New SqlParameter("@deptid", curDeptID))

Cmda.Parameters.Add(New SqlParameter("@empid", curEmpID))

'output parameters

Dim projectcnt As SqlParameter = Cmda.Parameters.Add("@projectcnt",
SqlDbType.Int)

projectcnt.Direction = ParameterDirection.Output

Dim curProCount As Integer

Try

If MyConn.State <> True Then

MyConn.Open()

End If

objDR = Cmda.ExecuteReader(System.Data.CommandBehavior.Clo seConnection)

While objDR.Read

curProCount = CInt(Cmda.Parameters("@projectcnt").Value)

End While

objDR.Close()

Catch e As SqlException

errpanel.Visible = True

warningpanel.Visible = False

mainpanel.Visible = False

errmessage.Text = e.Message

Finally

If MyConn.State = ConnectionState.Open Then

MyConn.Close()

End If

End Try

End Sub



Feb 1 '06 #1
1 1786
Hi All,

Nevermind :) I figured it out right after I posted this :P hehe

The problem was that I was trying to access the output parameters BEFORE
closing out the datareader DUH!
So i simply removed the while loop, moved the close() method up and then
grabbed the parameter value :)
Thx for being there anyways:)
"Joe Van Meer" <jv******@eastlink.ca> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi all,

I have an app that currently runs through 3 seperate stored procedures each returning a count of records. What I would like to do is combine these
calls into one call, however I am having an issue getting the output
parameters' values after execution. Here is a snipit of code that calls one for simplicity's sake: I think I am close, i was under the impression I
could use the datareader to get at it, but is is always returning 0 :)

Cheers & thanks a bunch, Joe :)
'SQL

'TOP LEVEL CALLING SP
CREATE PROCEDURE [dbo].[desktopquery]

@empid Int,
@deptid Int

AS

EXEC empprojectcnt @empid Int, @projectcnt Int Output
GO

'PROJECTS
CREATE PROCEDURE [dbo].[empprojectcnt]

@empid Int,
@projectcnt Int Output

AS

Select @projectcnt = COUNT(projectid) FROM PROJECT WHERE empid = @empid
Return @projectcnt
GO

'ASP NET

Private Sub GetEmpStats()

Dim curEmpID As Integer = CInt(Session("empid"))

Dim curDeptID As Integer = CInt(Session("deptid"))

Dim MyConn As New
SqlConnection(System.Configuration.ConfigurationSe ttings.AppSettings("dbConn "))

Dim MySQLe As String = "[dbo].[desktopquery]"

Dim Cmda As New SqlCommand(MySQLe, MyConn)

Cmda.CommandType = CommandType.StoredProcedure

Dim objDR As SqlDataReader

'INPUT parameters

Cmda.Parameters.Add(New SqlParameter("@deptid", curDeptID))

Cmda.Parameters.Add(New SqlParameter("@empid", curEmpID))

'output parameters

Dim projectcnt As SqlParameter = Cmda.Parameters.Add("@projectcnt",
SqlDbType.Int)

projectcnt.Direction = ParameterDirection.Output

Dim curProCount As Integer

Try

If MyConn.State <> True Then

MyConn.Open()

End If

objDR = Cmda.ExecuteReader(System.Data.CommandBehavior.Clo seConnection)

While objDR.Read

curProCount = CInt(Cmda.Parameters("@projectcnt").Value)

End While

objDR.Close()

Catch e As SqlException

errpanel.Visible = True

warningpanel.Visible = False

mainpanel.Visible = False

errmessage.Text = e.Message

Finally

If MyConn.State = ConnectionState.Open Then

MyConn.Close()

End If

End Try

End Sub



Feb 1 '06 #2

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

Similar topics

6
by: Samuel Hon | last post by:
Hi I'm not sure what the best approach for this is: I have a stored procedure which I would like to use to return several output values instead of returning a recordset. CREATE PROCEDURE...
3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
0
by: sbest | last post by:
Hi all, I am trying to execute a MS-SQL 2000 stored proedure from PHP 4.2.2 and return the single row of 3 output values. I've whittled down the error messages so that I'm now mainly getting an...
0
by: Sahadev K | last post by:
Here is the question 1. I have two stored procedures P1 and P2. 2. I want to call stored procedure P2 from P1. 3. Stored Procedure P2 returns a result set(as an open cursor). 4. In P1, I want to...
6
by: David Lozzi | last post by:
Here is the proc: CREATE PROCEDURE . @CID as int, @Netname as nvarchar(25), @Return as int OUTPUT AS IF EXISTS (SELECT DISTINCT netname FROM computers WHERE CompanyID = @CID AND...
5
by: Wael | last post by:
Hi, I have the following stored procedure that does some processing and puts the result in a temporary table. I tried several things that procedure to display output that I can access with...
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: codefragment | last post by:
Hi I have a chunky bit of sql that I will want to call from a number of places. It will return a few thousand rows. Whats the best way of structuring this? 1) I initially thought of using...
10
by: pinman | last post by:
hi i am trying to implemement forms authentication for my website but can't seem to get the stored procedure to output the correct value when checking a users credentials. the code is ALTER...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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,...

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.