473,387 Members | 1,545 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,387 software developers and data experts.

Obtaining the return code from a stored procedure

Hi,

Is there a way to obtain the return code from a stored procedure?

Here is what I have so far. The procedure executes, but I'm not able to
find the return code from the procedure.

Thanks,
Scott

SqlConnection sqlConn = new SqlConnection(strDbConn);
SqlCommand sqlCmd = new SqlCommand("usp_proc_name", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;

SqlParameter param1 = new SqlParameter("@PARAM_1", SqlDbType.VarChar, 100);
param1.Value = field1.Text;
sqlCmd.Parameters.Add(param1);

sqlCmd.Connection.Open();
SqlDataReader sqlReader =
sqlCmd.ExecuteReader(CommandBehavior.CloseConnecti on);
if (sqlReader.Read())
{
if (sqlReader.GetInt32(0) == 0)
{
// Procedure successul (expected)
}
else
{
// Procedure error occured (expected)
}
}
else
{
// Procedure error occured (expected)
** The debugger always lands here as I'm not doing something right **
}

CREATE PROCEDURE [dbo].[usp_proc_name]
@PARAM_1 varchar(100)
AS

INSERT INTO [dbo].[table]
(
...
)
VALUES
(
...
)

SELECT @error_num = @@ERROR

RETURN @error_num
GO

Nov 18 '05 #1
2 5171
Yes you can get the return value from sproc.

sqlCmd.Parameters.Add(New SqlParameter("RETURN_VALUE",
SqlDbType.Int)).Direction = ParameterDirection.ReturnValue

The name of the parameter is not critacal (notice I am not even using the @
character).

Pretty sure you cannot read the value back (any parameters for that matter)
until the datareader is closed!

Dim i as integer = CType(sqlCmd.Parameters("RETURN_VALUE").Value, Integer)

Looking at your sproc, you don't even want to use a datareader here. Try
this instead:

Dim cn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))
Dim cmd As SqlCommand = New SqlCommand("dbo.usp_SomeSproc", cn)
cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.Add(New SqlParameter("@SomeID",
SqlDbType.Int)).Value = someID
cmd.Parameters.Add(New SqlParameter("RETURN_VALUE",
SqlDbType.Int)).Direction = ParameterDirection.ReturnValue

Try
cn.Open()
cmd.ExecuteNonQuery()
Catch ex As SqlException
Throw New Exception("some error message", ex)
Finally
cn.Close()
End Try

Dim SomeReturnValue As Integer =
CType(cmd.Parameters("RETURN_VALUE").Value, Integer)

HTH,
Greg

"Scott Natwick" <no**********@yahoo.com> wrote in message
news:dP********************@comcast.com...
Hi,

Is there a way to obtain the return code from a stored procedure?

Here is what I have so far. The procedure executes, but I'm not able to
find the return code from the procedure.

Thanks,
Scott

SqlConnection sqlConn = new SqlConnection(strDbConn);
SqlCommand sqlCmd = new SqlCommand("usp_proc_name", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;

SqlParameter param1 = new SqlParameter("@PARAM_1", SqlDbType.VarChar,
100);
param1.Value = field1.Text;
sqlCmd.Parameters.Add(param1);

sqlCmd.Connection.Open();
SqlDataReader sqlReader =
sqlCmd.ExecuteReader(CommandBehavior.CloseConnecti on);
if (sqlReader.Read())
{
if (sqlReader.GetInt32(0) == 0)
{
// Procedure successul (expected)
}
else
{
// Procedure error occured (expected)
}
}
else
{
// Procedure error occured (expected)
** The debugger always lands here as I'm not doing something right **
}

CREATE PROCEDURE [dbo].[usp_proc_name]
@PARAM_1 varchar(100)
AS

INSERT INTO [dbo].[table]
(
...
)
VALUES
(
...
)

SELECT @error_num = @@ERROR

RETURN @error_num
GO

Nov 18 '05 #2
Greg,

Your suggestion works perfectly! Here is how I modified the code:

try
{
sqlCmd.Connection.Open();
sqlCmd.ExecuteNonQuery();
}
catch (Exception objException)
{
...
}
finally
{
sqlCmd.Connection.Close();
}
int nReturnCode = (int)sqlCmd.Parameters["@R_RETURN_CODE"].Value;

Thanks again!
Scott Natwick

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:eL*************@tk2msftngp13.phx.gbl...
Yes you can get the return value from sproc.

sqlCmd.Parameters.Add(New SqlParameter("RETURN_VALUE",
SqlDbType.Int)).Direction = ParameterDirection.ReturnValue

The name of the parameter is not critacal (notice I am not even using the
@ character).

Pretty sure you cannot read the value back (any parameters for that
matter) until the datareader is closed!

Dim i as integer = CType(sqlCmd.Parameters("RETURN_VALUE").Value, Integer)

Looking at your sproc, you don't even want to use a datareader here. Try
this instead:

Dim cn As SqlConnection = New
SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))
Dim cmd As SqlCommand = New SqlCommand("dbo.usp_SomeSproc", cn)
cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.Add(New SqlParameter("@SomeID",
SqlDbType.Int)).Value = someID
cmd.Parameters.Add(New SqlParameter("RETURN_VALUE",
SqlDbType.Int)).Direction = ParameterDirection.ReturnValue

Try
cn.Open()
cmd.ExecuteNonQuery()
Catch ex As SqlException
Throw New Exception("some error message", ex)
Finally
cn.Close()
End Try

Dim SomeReturnValue As Integer =
CType(cmd.Parameters("RETURN_VALUE").Value, Integer)

HTH,
Greg

"Scott Natwick" <no**********@yahoo.com> wrote in message
news:dP********************@comcast.com...
Hi,

Is there a way to obtain the return code from a stored procedure?

Here is what I have so far. The procedure executes, but I'm not able to
find the return code from the procedure.

Thanks,
Scott

SqlConnection sqlConn = new SqlConnection(strDbConn);
SqlCommand sqlCmd = new SqlCommand("usp_proc_name", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;

SqlParameter param1 = new SqlParameter("@PARAM_1", SqlDbType.VarChar,
100);
param1.Value = field1.Text;
sqlCmd.Parameters.Add(param1);

sqlCmd.Connection.Open();
SqlDataReader sqlReader =
sqlCmd.ExecuteReader(CommandBehavior.CloseConnecti on);
if (sqlReader.Read())
{
if (sqlReader.GetInt32(0) == 0)
{
// Procedure successul (expected)
}
else
{
// Procedure error occured (expected)
}
}
else
{
// Procedure error occured (expected)
** The debugger always lands here as I'm not doing something right **
}

CREATE PROCEDURE [dbo].[usp_proc_name]
@PARAM_1 varchar(100)
AS

INSERT INTO [dbo].[table]
(
...
)
VALUES
(
...
)

SELECT @error_num = @@ERROR

RETURN @error_num
GO


Nov 18 '05 #3

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

Similar topics

9
by: Roger Withnell | last post by:
I'm inserting a new record into an MS SQL database table and I want to obtain the new records autonumber immediately afterwards, as follows: MadminRS.CursorLocation = adUseServer...
2
by: Daniel | last post by:
hi ng, i am newbie to sqlserver and my problem seems simple, but i didn't find information about it: How can i display the RETURN @x value of a stored procedure in the sql analyzer of the...
2
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info...
5
by: Sandy | last post by:
Hello - I need a good example of how to take a return value from a stored procedure and use it in vb code. I have an app that searches a database by city and state. If a user makes a typo, the...
8
by: jbonifacejr | last post by:
Hi. I'm sorry to bother all of you, but I have spent two days looking at code samples all over the internet, and I can not get a single one of them to work for me. I am simply trying to get a value...
5
by: Fir5tSight | last post by:
Hi All, I have a C#.NET code as follows: private void ScanInput_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { try { Row lRow = this.Connection.InsertScannedFile(ID);
12
by: Dooza | last post by:
I have a stored procedure that takes a number of inputs, does a bulk insert, and then outputs a recordset. When I run the stored procedure in Server Management Studio I also get a return value from...
7
by: bootsy | last post by:
I get a timeout error using the code below. The Utils.GetDataTableForUI is calling a stored procedure(dbo.ConferencesSEL. The stored procedure does a simple select statement. Can someone tell me what...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...
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,...

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.