Results 1 to 4 of 4

Thread: [Resolved] Read RETURN val from Stored Procedure

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489

    Resolved [Resolved] Read RETURN val from Stored Procedure

    Normally I use a DataReader to read values from a stored procedure but it is not working in this situation. On a stored procedure that INSERTS into a tabel I have a RETURN statement that returns the @@IDENTITY (identity value for the inserted row, primary key). When I use the code below:
    VB Code:
    1. dr = cmd.ExecuteReader
    2. MsgBox(dr.GetValue(0))
    I get an error that says there is nothing to read. Anybody know how to read this value?
    Last edited by jcfowl; Mar 17th, 2005 at 11:32 AM. Reason: Resolved

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489

    Re: Read RETURN val from Stored Procedure

    The SQL code below works in SQL Query Analyzer. The stored procedure below Inserts the values passed and RETURNS the @@IDENTITY. How can I get the return value in .Net?


    DECLARE @retval as bigint
    exec @retval = spColonyContacts_INS 0,0,'',0,0,'','','',''
    print @retval

  3. #3
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Read RETURN val from Stored Procedure

    In your situation i would use a .executescalar and modify your stored procedure to include a select statement before your return

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489

    Re: Read RETURN val from Stored Procedure

    Got it, thanks to this link:
    http://msdn.microsoft.com/library/de...l/gazoutas.asp
    VB Code:
    1. cmd.Parameters.Add("@ReturnVal", SqlDbType.BigInt).Direction = ParameterDirection.ReturnValue
    I added the parameter to my command object and you don't even need the datareader. Here is all the code to get the return value of the stored procedure:
    VB Code:
    1. Dim cmd As New SqlClient.SqlCommand
    2. Dim conn As New SqlClient.SqlConnection(modGlobal.g_ConnString)
    3.  
    4. cmd.CommandType = CommandType.StoredProcedure
    5. 'Add all parameters needed for insert statement
    6. cmd.Parameters.Add("@ReturnVal", SqlDbType.BigInt).Direction = ParameterDirection.ReturnValue
    7. cmd.Connection = conn
    8. If conn.State <> ConnectionState.Open Then conn.Open()
    9. cmd.ExecuteNonQuery()
    10. MsgBox(cmd.Parameters("@ReturnVal").Value)
    Last edited by jcfowl; Mar 17th, 2005 at 11:34 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width