Subject RE: [IBO] How to Detect Whether Conection is Lost.
Author Joseph Carney
We wrap all our inserts, updates and deletes in try..except clauses, we then
pass the exception
to a custom error handling function which pops up a dialog asking the user
if they want to
attempt to reconnect or quit the application. They can repeatedly try to
reconnect until they get
connected. If the reconnect is succesful, we reopen all the appropriate
queries.
It's the only way we've found so far that handles the lost connection issue
properly.
Note: we use a Datamodule with all forms calling the connections and queries
from it.
Note2:the myconnect reference is a custom record type that holds connection
paramaters
retrieved from either the registy or an INI file at form startup. the info
gets passed
to any forms called by the main form.

Here is some sample code. It's a function that returns a boolean value.
Please ignore some of application specific code.
the msgshow reference is simply a non db memo field.

One simple way to handle this, is through a timer that periodically checks
the connection status of your IB_Connection and takes appropriate action

function TGS.ReconToApass: boolean;
begin
result := False;
if GuardDM.ApassLocal.Connected then
begin
bisconnect := True;
result := True;
end
else
with GuardDM do
begin
with Apasslocal do begin
timercheckfile.enabled := false;
Connected := false;
UserName := MyConnect.UserID;
Password := MyConnect.UserPWord;
Database := sDBConnect;
beep;
beep;
MsgShow.Clear;
MsgShow.Color := clRed;
MsgShow.Lines.Add('Attempting to Re-connect to APASS');
//here is the code to try to connect
try
Connect;
except
on E: EIB_ISCError do
begin
beep;
beep;
MsgShow.Clear;
MsgShow.Lines.Add('Unable to connect to A-PASS');
MsgShow.Lines.Add('Please contact support!');
bisconnect := false;
result := false;
end;
end;
//here is the code if the connection is successful
if Connected then
begin
qWhosOff.Active := false;
qWhosOff.Active := true;
if not qPASSLIST.Active then
qPASSLIST.Active := true;
if qActivity.Active then
qActivity.Active := false;
qActivity.Prepare;
qActivity.ParamByName('STATION_ID').AsString := sUserName;
qActivity.Active := true;
if not qSearchPass.Active then
qSearchPass.Active := true;
beep;
beep;
MsgShow.Clear;
MsgShow.Color := clWhite;
MsgShow.Lines.Add('Re-connected to A-PASS');
timercheckfile.enabled := true;
bisconnect := true;
result := true;
end;
end;
end;
end;

here is moe generic procedure that specifically references the
not connected error code. You can call it from anywhere.
this specific version works great with a timer or a call within an exception
block.
procedure TGS.HandleError(E: Exception);
const
ERRMESSAGE = 'Lost connection to the database. Attempt to reconnect?';
var
IsConnected: boolean;
begin
if E is EIB_ISCError then
begin
if EIB_ISCError(E).ERRCODE = 335544721 then
begin
// we lost connection to the database
// offer to retry or fail
// retry will try to connect
// cancel will shut down the app
timercheckFile.Enabled := False;
IsConnected := False;
while not IsConnected do
begin
if messagedlg(ERRMESSAGE, mtError, [mbCancel, mbRetry], 0) = mrRetry
then
begin
try
with GuardDM do
begin
apasslocal.Connected := False;

apasslocal.UserName := MyConnect.UserID;
apasslocal.Password := MyConnect.UserPWord;
apasslocal.Database := MyConnect.dbConnect;

apasslocal.Connect;
IsConnected := True;

timercheckFile.Enabled := True;
qWhosOff.Active := false;
qWhosOff.Active := true;
if not qPASSLIST.Active then
qPASSLIST.Active := true;
if qActivity.Active then
qActivity.Active := false;
qActivity.Prepare;
qActivity.ParamByName('STATION_ID').AsString := sUserName;
qActivity.Active := true;
if not qSearchPass.Active then
qSearchPass.Active := true;
end;
except
IsConnected := False;
end
end
else
begin
application.Terminate;
break;
end;
end;
end;
end;
end;

Either approach can be modified to meet a specific need.

hope this helps

Joe Carney.
Security Identification Systems Corporation "SISCO" is a state of the art
security integration and manufacturing company. Please send all inquiries to
info@....