Your Stored procedure should be like below. See for Error_Message
and Error_Number
Create Proc Testing
As
Set NoCount On
Set XACT_ABORT ON
Begin Try
Begin Tran
"Your Insert/Update Statement"
Select '' as 'Message', 0 as 'Number'
Commit Tran
End Try
Begin Catch
Select Error_Message() as 'Message', Error_Number() as 'Number'
Rollback Tran
End Catch
Your code should be like below
string str;
int number;
using (System.Data.SqlClient.SqlConnection con = new SqlConnection("YourConnection string")) {
con.Open();
SqlCommand cmd = new SqlCommand();
string expression = "Parameter value";
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Your Stored Procedure";
cmd.Parameters.Add("Your Parameter Name",
SqlDbType.VarChar).Value = expression;
cmd.Connection = con;
using (IDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
str = dr["Message"].ToString();
number = Convert.ToInt16(dr["Number"]);
}
}
}
In this way you can capture the Error Message/ Error Number from stored procedure if it returns any