try and catch in a Transaction

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

Would someone tell me why errors happen in TestCommand.ExecuteNonQuery and
the application just hang in there, it won't go to the catch. Why?
Thanks for help.


Jason



try
{
TestCommand.Transaction=myTrans;
TestCommand.ExecuteNonQuery();
}
catch (SqlException EQry)
{
MessageBox.Show(String.Format("An exception occurred" +
" : {0}. Please contact your system administrator.", EQry.Message));
}
 
Hi Jason,

Currently, the catch operator catches exceptions of type
System.Data.SqlClient.SqlException that occur in the
SqlCommand.ExecuteNonQuery() call [1]. Most likely the exception that is
thrown (and unhandled) is not of the aforementioned type.

You can append another catch statement for handling exceptions of type
System.InvalidOperationException and/or System.Exception, because they can be
thrown in certain circumstances, too.

[1] SqlCommand Documentation:
http://msdn2.microsoft.com/en-us/library/z4956bkc.aspx
 
Thanks Stanimir.
I now also have the System.InvalidOperationException and System.Exception on
the catch,
and it is System.FormaException shows up on the MessageBox.
But is it possible to know what data/field brings out the error?
Thanks for help.


Jason

Hi Jason,

Currently, the catch operator catches exceptions of type
System.Data.SqlClient.SqlException that occur in the
SqlCommand.ExecuteNonQuery() call [1]. Most likely the exception that is
thrown (and unhandled) is not of the aforementioned type.

You can append another catch statement for handling exceptions of type
System.InvalidOperationException and/or System.Exception, because they can
be
thrown in certain circumstances, too.

[1] SqlCommand Documentation:
http://msdn2.microsoft.com/en-us/library/z4956bkc.aspx

--
Stanimir Stoyanov
www.stoyanoff.info


Jason Huang said:
Hi,

Would someone tell me why errors happen in TestCommand.ExecuteNonQuery
and
the application just hang in there, it won't go to the catch. Why?
Thanks for help.


Jason



try
{
TestCommand.Transaction=myTrans;
TestCommand.ExecuteNonQuery();
}
catch (SqlException EQry)
{
MessageBox.Show(String.Format("An exception occurred" +
" : {0}. Please contact your system administrator.",
EQry.Message));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top