Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6018393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:16:08+00:00 2026-05-23T03:16:08+00:00

OK so this might sound a bit weird! The thing is that in a

  • 0

OK so this might sound a bit weird!
The thing is that in a project there are many stored procedure already written like this:

CREATE PROCEDURE [Status_Insert]

        @StatusId       int OUTPUT,
        @Status         nvarchar (50),
        @IsDeleted      bit = 0

AS

SET NOCOUNT ON;

BEGIN TRY
BEGIN TRAN

INSERT INTO dbo.[Status]
(
        [Status],
        [IsDeleted]
)
VALUES
(
        @Status,
        @IsDeleted
)

SET @StatusId = @@IDENTITY

COMMIT TRAN
RETURN 1
END TRY

BEGIN CATCH
ROLLBACK TRAN

DECLARE @ErrorNumber_INT INT;
DECLARE @ErrorSeverity_INT INT;
DECLARE @ErrorProcedure_VC VARCHAR(200);
DECLARE @ErrorLine_INT INT;
DECLARE @ErrorMessage_NVC NVARCHAR(4000);

SELECT
        @ErrorMessage_NVC = ERROR_MESSAGE(),
        @ErrorSeverity_INT = ERROR_SEVERITY(),
        @ErrorNumber_INT = ERROR_NUMBER(),
        @ErrorProcedure_VC = ERROR_PROCEDURE(),
        @ErrorLine_INT = ERROR_LINE()

RETURN -1
END CATCH

The primary key is an Identity and an output variable is used to retrieve its value after the insert statement. (By the way, is this approach a good practice?)
Now the return values are used to indicate the success or failure of the procedure. (IE. if the operation was successful 1 is returned and -1 is returned if the operation has failed)
If there is an error in the execution of the procedure, the last select statement will return the error to the user.

Now how can I call this procedure in C# and get these results at the same time?
What I want to do is, execute the procedure, and get the return value, if it was 1 then get the result of the last select statement (which contains more info about the error that has occured)
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:16:09+00:00Added an answer on May 23, 2026 at 3:16 am

    You can use “ReturnValue” parameter in Command.

    using(var connection = GetOpenConnection()) {
        using(var cmd = connection.CreateCommand()){
            cmd.CommandText = "Status_Insert";
            cmd.CommandType = CommandType.StoredProcedure;
    
            var prmReturnValue = new SqlParameter( "@ReturnValue", DBNull.Value );
            prmReturnValue.Direction = ParameterDirection.ReturnValue;
    
            using(var reader = cmd.ExecuteReader()) {
                // process table result(s)
            }
    
            var returnValue = prmReturnValue.Value;
        }
    }
    

    EDIT:
    The problem is in select. If you select something into variable, the select will not be in result-sets.

    If you want assign variable(s) from functions you don’t need SELECT statement, you can use SET command or you can initialize values in DECLARE statement (from version 2008):

    /* variable initialization in DECLARE statements >>> */
    DECLARE @ErrorNumber_INT INT              = ERROR_NUMBER();
    DECLARE @ErrorSeverity_INT INT            = ERROR_SEVERITY();
    DECLARE @ErrorProcedure_VC VARCHAR(200)   = ERROR_PROCEDURE();
    /* <<< variable initialization in DECLARE statements */
    
    /* variable initialization with SET statements >>> */
    DECLARE @ErrorLine_INT INT;
    DECLARE @ErrorMessage_NVC NVARCHAR(4000);
    SET @ErrorLine_INT INT = ERROR_LINE();
    SET @ErrorMessage_NVC  = ERROR_MESSAGE();
    /* <<< variable initialization with SET statements */
    
    /* this select will be in result-set(s) >>> */
    SELECT
            @ErrorNumber_INT AS [ErrorNumber_INT],
            @ErrorSeverity_INT AS [ErrorSeverity_INT],
            @ErrorProcedure_VC AS [ErrorProcedure_VC],
            @ErrorLine_INT AS [ErrorLine_INT],
            @ErrorMessage_NVC AS [ErrorMessage_NVC]
    /* <<< this select will be in result-set(s) */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Um, this might sound a bit weird. We were having some problems with a
This might sound a bit dumb. I always had this impression that web.config should
I might sound a bit weird but, is there a way? For example, I
I know this question might sound a little bit crazy, but I tough that
This might sound like a little bit of a crazy question, but how can
This might sound like a bit of a silly question but I been looking
Yes, this might sound like a newbie question but there's a TWIST! ( And
This might sound a bit of an odd question but I know what I
This might sound like a stupid question but just trying to learn something here.
This might sound like a reaaaally dumb question but... why do browsers have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.