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.
You can use “ReturnValue” parameter in Command.
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):