home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 March
/
Chip_1998-03_cd.bin
/
zkuste
/
delphi
/
ruzkomp
/
BDEERROR.ZIP
/
DBENGINE.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-03-15
|
7KB
|
218 lines
unit dbengine;
// Delphi 2 Database error handling example
// Borland International 1996
// Simply include this unit in your uses clause and call the HandleException
// method when necessary.
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, DB,
StdCtrls, ExtCtrls, Buttons;
type
TDBError = class(TForm)
Panel1: TPanel;
Error1: TLabel;
Error2: TLabel;
Error3: TLabel;
Error4: TLabel;
Panel2: TPanel;
NativeCode: TLabel;
DoneBtn: TBitBtn;
NatBtn: TBitBtn;
procedure Button1Click(Sender: TObject);
procedure SizeErrorDialog(MaxStringSize: integer; Errors: word);
procedure NatBtnClick(Sender: TObject);
private
{ Private declarations }
procedure ResetControls(Title: string);
procedure CreateDBEngineError(E: EDBEngineError);
procedure ShowEDatabaseError(E: EDatabaseError);
procedure EnableErrorBox(Count: integer; Enable, Visible: boolean);
public
{ Public declarations }
procedure HandleException(E: Exception);
end;
var
DBError: TDBError;
implementation
{$R *.DFM}
// Reset controls to default state.
procedure TDBError.ResetControls(Title: string);
begin
Error1.Visible := False;
Error2.Visible := False;
Error3.Visible := False;
Error4.Visible := False;
Error1.AutoSize := True;
Error2.AutoSize := True;
Error3.AutoSize := True;
Error4.AutoSize := True;
NatBtn.Enabled := False;
Caption := Title;
end;
// This procedure is called when a EDatabaseError has occurred. Simply
// display the message in the error dialog.
procedure TDBError.ShowEDatabaseError(E: EDatabaseError);
var
StringSize: integer;
begin
ResetControls('an EDatabase Error has occured');
Error1.Caption := E.Message + '.';
StringSize := Canvas.TextWidth(Error1.Caption);
SizeErrorDialog(StringSize, 1);
EnableErrorBox(1, True, False);
end;
// Determine what type of exception was raised and call the appropriate method.
procedure TDBError.HandleException(E: Exception);
begin
if E is EDBEngineError then
DBError.CreateDBEngineError(EDBEngineError(E))
else
if E is EDatabaseError then
ShowEDatabaseError(EDatabaseError(E))
else
Application.ShowException(E);
end;
// Setup and display the error dialog box.
procedure TDBError.EnableErrorBox(Count: integer; Enable, Visible: boolean);
begin
Error1.AutoSize := False;
Error2.AutoSize := False;
Error3.AutoSize := False;
Error4.AutoSize := False;
Error1.Height := 13;
Error2.Height := 13;
Error3.Height := 13;
Error4.Height := 13;
if Count >= 1 then
Error1.Visible := True;
if Count >= 2 then
Error2.Visible := True;
if Count >= 3 then
Error3.Visible := True;
if Count >= 4 then
Error4.Visible := True;
NatBtn.Enabled := Enable;
NatBtn.Visible := Visible;
ShowModal;
end;
// Make the size of the dialog box just big enough to show the message string.
procedure TDBError.SizeErrorDialog(MaxStringSize: integer; Errors: word);
begin
NativeCode.Width := 0;
Panel1.Width := MaxStringSize + 16;
Width := MaxStringSize + 30;
case Errors of
1: Panel1.Height := 30;
2: Panel1.Height := 44;
3: Panel1.Height := 58;
4: Panel1.Height := 72;
end;
Height := Panel1.Height + 63;
DoneBtn.Top := Height - 53;
NatBtn.Top := DoneBtn.Top;
end;
// Create the error messages for the EDBEngineError exception.
procedure TDBError.CreateDBEngineError(E: EDBEngineError);
var
StringSize: Integer;
ButtonEnable: boolean;
begin
ResetControls('an EDBEngine Error has occured');
ButtonEnable := False;
Error1.Visible := False;
Error2.Visible := False;
Error3.Visible := False;
Error4.Visible := False;
NatBtn.Enabled := False;
StringSize := 0;
NativeCode.Caption := 'Native Error Code(s) ';
if E.ErrorCount >= 1 then
begin
Error1.Caption := Format('Entry: 0, Error Number: %d, %s',
[E.Errors[0].ErrorCode, E.Errors[0].Message]);
StringSize := Canvas.TextWidth(Error1.Caption);
if E.Errors[0].NativeError <> 0 then
begin
ButtonEnable := True;
NativeCode.Caption := Format('%s %d, %s',
[NativeCode.Caption, E.Errors[0].NativeError, E.Errors[0].Message]);
end;
end;
if E.ErrorCount >= 2 then
begin
Error2.Caption := Format('Entry: 1, Error Number: %d, %s',
[E.Errors[1].ErrorCode, E.Errors[1].Message]);
if Canvas.TextWidth(Error2.Caption) > StringSize then
StringSize := Canvas.TextWidth(Error2.Caption);
if E.Errors[1].NativeError <> 0 then
begin
ButtonEnable := True;
NativeCode.Caption := Format('%s %d, %s',
[NativeCode.Caption, E.Errors[1].NativeError, E.Errors[1].Message]);
end;
end;
if E.ErrorCount >= 3 then
begin
Error3.Caption := Format('Entry: 2, Error Number: %d, %s',
[E.Errors[2].ErrorCode, E.Errors[2].Message]);
if Canvas.TextWidth(Error3.Caption) > StringSize then
StringSize := Canvas.TextWidth(Error3.Caption);
if E.Errors[2].NativeError <> 0 then
begin
ButtonEnable := True;
NativeCode.Caption := Format('%s %d, %s',
[NativeCode.Caption, E.Errors[2].NativeError, E.Errors[2].Message]);
end;
end;
if E.ErrorCount >= 4 then
begin
Error4.Caption := Format('Entry: 3, Error Number: %d, %s',
[E.Errors[3].ErrorCode, E.Errors[3].Message]);
if Canvas.TextWidth(Error4.Caption) > StringSize then
StringSize := Canvas.TextWidth(Error4.Caption);
if E.Errors[3].NativeError <> 0 then
begin
ButtonEnable := True;
NativeCode.Caption := Format('%s %d, %s',
[NativeCode.Caption, E.Errors[3].NativeError, E.Errors[3].Message]);
end;
end;
SizeErrorDialog(StringSize, E.ErrorCount);
EnableErrorBox(E.ErrorCount, ButtonEnable, True);
end;
// If the user wants to display the native message, size the dialog box.
procedure TDBError.NatBtnClick(Sender: TObject);
begin
NatBtn.Enabled := False;
Panel2.Top := DoneBtn.Top + 31;
Panel2.Width := Panel1.Width;
Height := Height + 43;
NativeCode.Width := Panel2.Width - 15;
end;
// Close the error dialog.
procedure TDBError.Button1Click(Sender: TObject);
begin
Close;
end;
initialization
// Create the dialog box.
DBError := TDBError.Create(Application);
end.