Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Obtaining the return code from a stored procedure

Reply
Thread Tools

Obtaining the return code from a stored procedure

 
 
Scott Natwick
Guest
Posts: n/a
 
      10-02-2004
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



 
Reply With Quote
 
 
 
 
Greg Burns
Guest
Posts: n/a
 
      10-02-2004
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" <> wrote in message
news:dPSdndSde6mbm8LcRVn-...
> 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
>
>
>



 
Reply With Quote
 
 
 
 
Scott Natwick
Guest
Posts: n/a
 
      10-03-2004
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:...
> 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" <> wrote in message
> news:dPSdndSde6mbm8LcRVn-...
>> 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
>>
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Return value from Stored Procedure Dooza ASP General 16 09-17-2008 03:06 PM
Return Value from Stored procedure bbawa1@yahoo.com ASP .Net 2 06-19-2007 06:14 PM
Return Value from Stored procedure bbawa1@yahoo.com ASP .Net 1 06-18-2007 08:09 PM
stored procedure return value question philip ASP .Net 2 08-10-2006 03:42 PM
Getting Return Value of Stored Procedure Vipul Pathak ASP General 3 07-30-2003 01:51 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57