Partilhar via


Handling Database Errors

Database errors, also called "engine errors," occur when run-time errors occur in record-level event code. For example, a database error occurs when a user attempts to store a null value to a field that doesn't allow null values.

When a database error occurs, the underlying database engine that detects the error typically posts an error message. However, the exact nature of the error message depends on what database is being accessed — for example, the error messages produced by a remote database server (such as Microsoft SQL Server) will probably be different from those produced if a database error occurs in a local Visual FoxPro table.

In addition, engine-level errors are sometimes very generic, because the database engine has no information about the context in which a record is being updated. As a consequence, error messages produced by a database engine are often less useful to the end user of a Visual FoxPro application.

To handle database errors in a more application-specific way, you can create triggers using the CREATE TRIGGER command. The trigger is called when a record update is attempted (delete, insert, or update). Your custom trigger code can then look for application-specific error conditions and report these.

If you handle database errors using triggers, you should turn buffering on. That way, when a record is updated your trigger is called, but the record is not immediately sent to the underlying database. You therefore avoid the possibility of producing two error messages: one from your trigger, and another from the underlying database engine.

To create custom error messages using triggers

  1. Inside a user-defined function or stored procedure, write your own message text.
  2. Enable buffering with the CURSORSETPROP( ) function to display your custom text. If buffering is off, the user will see both your custom text and the engine error message.

See Also

Referencing Multiple Databases | Managing a Database | Validating a Database | Developing Databases | Validating a Database | Working with a Database