I am new to this forum but please bear with me. I have a c# Windows form which has two checkboxes on it One is called chkThrowError and the other is called chkDivideError, both are unchecked. These controls are purely there to control execution of a stored procedure. I have a command Button with the following Code:
private void cmdError_Click(object sender, EventArgs e)
{
int ThrowError = 0;
int DividByZero = 0;
if (chkThrowError.Checked)
{
ThrowError = 1;
}
if (chkDivideError.Checked)
{
DividByZero = 1;
}
try
{
clsBBFinances.TestError(ThrowError,DividByZero);
MessageBox.Show("Everything is Hunkey Dorey");
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
clsBBFinances is my Business Object and the TestError routine is as follows:
public static void TestError(int ThrowError, int ThrowDivideError)
{
System.Data.SqlClient.SqlDataReader objRs = null;
string strsql = "";
bool Success = true;
string ErrMsg = "";
int intCount = 0;
try
{
strsql = strsql + "Exec ";
strsql = strsql + "TestError " + ThrowError.ToString() + " , " + ThrowDivideError.ToString();
objRs = clsBBFinances.TheDatabase.ExecuteReadOnly(strsql);
while (objRs.Read())
{
intCount++;
object xxx = objRs[0];
}
Success = true;
ErrMsg = "";
//intCount = objRs.RecordsAffected;
}
catch (Exception ex)
{
ErrMsg = ex.Message;
intCount = 0;
Success = false;
throw ex;
}
finally
{
if (objRs != null)
{
if (!objRs.IsClosed)
{
objRs.Close();
}
}
}
}
Please assume the syntax to be correct and I have a valid SQL connection (sql2008(R2))
This basically calls the following Stored Procedure:
Create PROCEDURE TestError
(@ThrowError int=0
,@ThrowDivideError int= 0
)
AS
BEGIN
SET NOCOUNT ON;
Declare @x int
Declare @y int
Declare @ErrorNumber int
Declare @ErrorMessage varchar(2000)
Declare @ErrorSeverity int
Declare @ErrorState int
Begin Try
If isnull(@ThrowError,0) <> 0
Begin
RaisError ('Forced Error Raised',16,1)
Return
End
If isnull(@ThrowDivideError,0) <>0
Begin
set @x = 5
Set @y =0
Select @x/@y as z
End
End Try
Begin catch
Select @ErrorNumber = Error_Number()
,@ErrorMessage = 'Test Error ' + Error_message()
,@Errorseverity= Error_Severity()
,@ErrorState = Error_State()
RaisError (@Errormessage, @ErrorSeverity, @ErrorState)
--THROW 51000, @Errormessage, @ErrorState;
Return
End Catch
Select * from information_Schema.tables
END
GO
Basically, I am raising an error dependant upon the parameter passed in.. If My first parameteer is non zero, I raise a custom error, and if the first is 0 and the second Non Zero then i execute a divide by zero to generate me a system Error. If Both are zero I do a simple select from Information_Schema.tables, just to finish it off. The purpose of the stored procedure is tocall MY error and a system error.
When I call the procedure and my first argument is set to 1, my custom error is raised, which goes through the SQl catch block and is subsequently trapped in my catch block in c#.
However, when I execute it with the second argument set to 1, the divide by zero error is trapped in the SQL catch block but it is NOT trapped by the catch block in c#. The code continues as if no error has occured.
I want to be able to catch the second error.. Does anybody know how. Please feel free to paste the above code and try it for yourselves.
Forced error I get the message Test Error Forced Error Raised
.
Divide By Zero I get the message Everything is Hunkey Dorey
, is NOT the case.
HELP needed please
You're using a SqlDataReader, it reads only one result set at a time. The exception happens in the second result set. So you have to move the reader to the second result set with NextResult(). Then you hit the exception...
var connection = new SqlConnection("Data Source=xxx;Initial Catalog=Test;User Id=xxx;Password=xxx");
connection.Open();
try
{
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "TestError";
command.Parameters.AddWithValue("@ThrowDivideError", 1);
var reader = command.ExecuteReader(CommandBehavior.CloseConnection);
reader.Read();
reader.NextResult();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
来源:https://stackoverflow.com/questions/16046065/sql-error-not-thrown-back-to-caller