Most methods of the Transaction object throw System.InvalidOperationException if the Transaction is already connected, or Sybase.DataWindow.TransactionException for other failures. You can wrap the Connect, Commit, and Rollback methods in try-catch statements that catch these exceptions.
The TransactionException object has two properties that you can use to help determine the cause of the failure, SqlDbCode and SqlErrorText.
Property |
Datatype |
Description |
|---|---|---|
SqlDbCode |
Int32 |
Database error code |
SqlErrorText |
String |
The text of the database vendor’s error message corresponding to the error code |
The string SqlErrorText in the Transaction object contains the database vendor-supplied error message. The integer named SqlDbCode in the Transaction object contains the database vendor-supplied status code. You can reference these variables in your code.
Example The following try-catch clause handles the exceptions that might be thrown when a Windows application connects to a database, associates a transaction with a DataWindowControl, and retrieves data from the database. Notice that DataWindow operations such as Retrieve might throw a DbErrorException, which also has SqlErrorText and SqlDbCode properties:
[Visual Basic] Try sqlca.Connect() dwDept.SetTransaction(sqlca) dwDept.Retrieve() Dim msg As String Catch Ex As System.InvalidOperationException msg = "The transaction is already connected" _ + "The application cannot continue" MsgBox(msg) Application.Exit() Catch Ex As TransactionException msg = Ex.SqlErrorText + _ "\n\nThe application cannot continue." + _ "Database Connection Failed" Application.Exit() Catch Ex As DbErrorException msg = Ex.SqlErrorText + _ "\n\nThe application cannot continue." + _ "DataWindow Operation Failed" Application.Exit() Catch Ex As System.Exception msg = Ex.ToString() + _ "\n\nThe application cannot continue." + _ "Unexpected Exception" Application.Exit() End Try
[C#]
try
{
sqlca.Connect();
dwDept.SetTransaction(sqlca);
dwDept.Retrieve();
}
catch (System.InvalidOperationException ex)
{
MessageBox.Show("The application cannot
continue.", "The transaction is already
connected", MessageBoxButtons.OK,
MessageBoxIcon.Stop);
Application.Exit();
}
catch (TransactionException ex)
{
MessageBox.Show(ex.SqlErrorText +
"\n\nThe application cannot continue.",
"Database Connection Failed",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}
catch (DbErrorException ex)
{
MessageBox.Show(ex.SqlErrorText +
"\n\nThe application cannot continue.",
"DataWindow Operation Failed",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString() +
"\n\nThe application cannot continue.",
"Unexpected Exception", MessageBoxButtons.OK,
MessageBoxIcon.Stop);
Application.Exit();
}