OnSQLError - cSQLExecutor

Triggered when an ODBC error occurs

Type: Event

Parameters: String sSqlState String sSqlMessage

ParameterDescription
sSqlStateODBC error number
sSqlMessageODBC error description


Syntax
Procedure OnSQLError String sSqlState String sSqlMessage

Description

Triggered when an ODBC error occurs.

By default, a DataFlex error will be generated.


Implementing this event without 'Forward Send' will cancel this error.

Object oSQLExecutor is a cSQLExecutor
    Set psConnectionId to "ID1"
    Move Self to ghoSQLExecutor    

    Procedure OnSQLError String sSQLState String sSQLMessage
        // Trapping this event cancels showing the error
    End_Procedure
End_Object


Alternatively, you could suppress the error by ignoring it:

Send Ignore_Error of Error_Object_Id DFERR_PROGRAM
Send SQLExecDirect of ghoSQLExecutor sStatement
Send Trap_Error of Error_Object_Id DFERR_PROGRAM


You can get the error information by calling psSQLMessage:

Get psSQLMessage of ghoSQLExecutor to sMessageText

Sample

String sMessage
Send Ignore_Error of Error_Object_Id DFERR_PROGRAM
Send SQLExecDirect of ghoSQLExecutor """
    DECLARE @a INT = 1000
    PRINT @a
    """
Get psSQLMessage of ghoSQLExecutor to sMessage
Send Info_Box sMessage
Send Trap_Error of Error_Object_Id DFERR_PROGRAM


The above code will contain the ODBC driver name and the value 1000 in sMessage, e.g. "[Microsoft][ODBC Driver 18 for SQL Server][SQL Server]1000". This is not an error message and therefore not trapped via OnSQLError.

To show what an error looks like, you can try the following:

String sMessage
Send Ignore_Error of Error_Object_Id DFERR_PROGRAM
Send SQLExecDirect of ghoSQLExecutor """
    DECLARE @a INT = 1000
    PRINT 'Your queue no is ' + @a
    """
Get psSQLMessage of ghoSQLExecutor to sMessage
Send Info_Box sMessage
Send Trap_Error of Error_Object_Id DFERR_PROGRAM