home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 March
/
Chip_1998-03_cd.bin
/
zkuste
/
delphi
/
ruzkomp
/
BDEERROR.ZIP
/
MAIN.PAS
< prev
Wrap
Pascal/Delphi Source File
|
1996-04-18
|
4KB
|
134 lines
unit main;
// Delphi 2 Database error handling example
// Borland International 1996
// This example includes a routine that can display more verbose database errors
// that occur in an application. In particular, the EDatabaseError and
// EDBEngineError are handled. If any other exception is raised, the standard
// dialog box is displayed with the exception message.
// To use this error handler, simply include the DBEngine unit in your uses
// clause. Use the try..except, try..finally, or the Application.OnException
// to overrive the default exception handler.
//
// Here is a simple example of code using a try..except block:
// try
// Table1.Open
// except
// DBError.HandleException(Exception(ExceptObject));
// end;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, DBGrids, ExtCtrls, DBCtrls, DB, DBTables, StdCtrls, DBEngine, BDE,
Menus;
type
TForm1 = class(TForm)
Table1: TTable;
MainMenu1: TMainMenu;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Exceptions1: TMenuItem;
KeyViolationPrimary1: TMenuItem;
KeyViolationSecondary1: TMenuItem;
FieldMissing1: TMenuItem;
NotinEditMode1: TMenuItem;
Table1CustNo: TFloatField;
Table1Company: TStringField;
Table1City: TStringField;
Database1: TDatabase;
InterBaseLoginFail1: TMenuItem;
procedure KeyViolationPrimary1Click(Sender: TObject);
procedure KeyViolationSecondary1Click(Sender: TObject);
procedure FieldMissing1Click(Sender: TObject);
procedure NotinEditMode1Click(Sender: TObject);
procedure InterBaseLoginFail1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
// Create a Primery key violation and display the message.
procedure TForm1.KeyViolationPrimary1Click(Sender: TObject);
var
Field1: integer;
begin
try
Table1.First;
Field1 := Table1.FieldByName('CustNo').AsInteger;
Table1.Insert;
Table1.FieldByName('CustNo').AsInteger := Field1;
Table1.FieldByName('Company').AsString := 'Not in table';
Table1.FieldByName('City').AsString := 'CA';
Table1.Post;
except
DBError.HandleException(Exception(ExceptObject));
Table1.Cancel;
end;
end;
// Create a Secondary key violation and display the message.
procedure TForm1.KeyViolationSecondary1Click(Sender: TObject);
var
Field2: string;
begin
try
Table1.First;
Field2 := Table1.FieldByName('Company').AsString;
Table1.Insert;
Table1.FieldByName('CustNo').AsInteger := 0;
Table1.FieldByName('Company').AsString := Field2;
Table1.FieldByName('City').AsString := 'CA';
Table1.Post;
except
DBError.HandleException(Exception(ExceptObject));
Table1.Cancel;
end;
end;
// Create a field is missing EDatabaseError and display the message.
procedure TForm1.FieldMissing1Click(Sender: TObject);
begin
try
Table1.Insert;
Table1.Post
except
DBError.HandleException(Exception(ExceptObject));
Table1.Cancel;
end;
end;
// Create a not in edit mode EDatabaseError and display the message.
procedure TForm1.NotinEditMode1Click(Sender: TObject);
begin
try
Table1.First;
Table1.FieldByName('CustNo').AsInteger := 100;
except
DBError.HandleException(Exception(ExceptObject));
end;
end;
procedure TForm1.InterBaseLoginFail1Click(Sender: TObject);
begin
try
Database1.Open;
except
DBError.HandleException(Exception(ExceptObject));
end;
end;
end.