The DbErrorException class

When using methods such as UpdateData and Retrieve, a database error can cause a DbErrorException to be thrown. For example, this can occur if you try to insert a row that does not have values for all columns that have been defined as not allowing null. Your exception-handling code can get the values of the parameters listed in Table 3-9.

Table 3-9: DbErrorException parameters

Parameter

Meaning

Buffer

The buffer containing the row involved in the database activity that caused the error.

RowNumber

The number of the row involved in the database activity that caused the error (the row being updated, selected, inserted, or deleted).

SqlDbCode

A database-specific error code. See your DBMS documentation for information on the meaning of the code.

SqlErrorText

A database-specific error message.

SqlSyntax

The full text of the SQL statement being sent to the DBMS when the error occurred.

Example

This Visual Basic code catches a DbErrorException and displays the SqlErrorText is a message box:

Try
   dwGrid.UpdateData()
Catch ex As Sybase.DataWindow.DbErrorException
   Dim sError As String
   sError = ex.SqlErrorText
   SQLCA.RollBack()
   MsgBox(sError)
   Return
End Try

This C# code displays a message box that uses the value of SqlDbCode:

// Database error -195 means that some of the
// required values are missing
If Ex.SqlDbCode = -195 {
   string message = "You have not supplied values for
      all the required fields.";
   string caption = "Missing Information";
   MessageBox.Show(message, caption);
}