Search code examples
c++builderfiredac

Abort TFDConnection in OnLogin() event?


I have my own password handler for the OnLogin event of a TFDConnection object. The return value of that handler is void so I can't tell it to abort the connection and optionally exit the program.

I would guess I would then have to throw something in some form to tell it to abort, but I didn't see anything in the documentation.

What is the proper way to abort the attempt to connect in OnLogin when the password dialog has a Cancel button? I'd like to just exit the application if it fails as well.


Solution

  • I would guess I would then have to throw something in some form to tell it to abort, but I didn't see anything in the documentation.

    Your guess is correct. Simply throw any exception you want.

    TFDConnection's default behavior when its own LoginDialog is canceled is to call the FDException() function to throw an EFDException exception that has its FDCode property set to er_FD_ClntDbLoginAborted.

    I'd like to just exit the application if it fails as well.

    Before exiting your OnLogin handler, you can call Application->Terminate() (or Application->MainForm->Close()). Or, whatever code is connecting the TFDConnection can catch the exception you throw, and then it can call Terminate() (or Close()), as demonstrated in the documentation:

    Establishing Connection (FireDAC) : Handling Connection Errors