Click here to Skip to main content
15,886,593 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to know that if transaction is failed in stored procedure then how to know in c# where stored procedure called?
SQL
ALTER PROCEDURE [dbo].[sp_eContractorChange] 
(
@UserId nvarchar(30)=null,

@Contractor nvarchar(30)=null,

@Mode char(1)
)
AS
BEGIN	
     if @Mode='U'
     begin
           BEGIN TRY
           BEGIN TRAN
           insert into eContractorTransLog(UserId,Contractor) values(@UserId,@Contractor);
           update TUser] set Department=@Contractor where UserID=@UserId;
           
           COMMIT TRAN
           END TRY
           BEGIN CATCH
           -- Execute error retrieval routine.
           -- EXECUTE usp_GetErrorInfo;
           ROLLBACK TRAN
     
           END CATCH
     end
END


C#
 using (cmd = new SqlCommand("sp_eContractorChange", con))
 {
      cmd.CommandType = CommandType.StoredProcedure;

                        cmd.Parameters.Add(new SqlParameter("@UserId", comboBoxUserId.Text.Trim()));
                        cmd.Parameters.Add(new SqlParameter("@Contractor", txtContractorName.Text.Trim()));


cmd.Parameters.Add(new SqlParameter("@Mode", "U"));
                        dr = cmd.ExecuteReader();
                      
                        if (dr != null)
                        {

                            Clear();
                            MessageBox.Show("User's contractor has been updated succesfully.");

                        }
}

Herre if tran failed it return dr not is equal to null ,how can be recognise in cE?
Posted

1 solution

What?
Why are you using ExecuteReader on a query that doesn;t return any rows regardless of if it succeeds or fails? ExecuteReader never returns null - it returns a Reader which may or may not have rows, but it can't return null.
In this case, it will always return a reader which contains no rows because at no point in your query do you select any rows!

Return a status code by all means:
SQL
...
        ROLLBACK TRAN
        RETURN -1
        END CATCH
    END
    RETURN 0
END
And use ExecuteScalar to perform the SP and return an integer result.

[edit]I can't spell "ExecuteScalar" this morning... - OriginalGriff[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900