home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / Chip_1998-03_cd.bin / zkuste / delphi / ruzkomp / BDEERROR.ZIP / MAIN.PAS < prev   
Pascal/Delphi Source File  |  1996-04-18  |  4KB  |  134 lines

  1. unit main;
  2. // Delphi 2 Database error handling example
  3. // Borland International 1996
  4. // This example includes a routine that can display more verbose database errors
  5. //   that occur in an application.  In particular, the EDatabaseError and
  6. //   EDBEngineError are handled.  If any other exception is raised, the standard
  7. //   dialog box is displayed with the exception message.
  8. // To use this error handler, simply include the DBEngine unit in your uses
  9. //   clause.  Use the try..except, try..finally, or the Application.OnException
  10. //   to overrive the default exception handler.
  11. //
  12. //  Here is a simple example of code using a try..except block:
  13. //  try
  14. //    Table1.Open
  15. //  except
  16. //    DBError.HandleException(Exception(ExceptObject));
  17. //  end;
  18.  
  19. interface
  20.  
  21. uses
  22.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  23.   Grids, DBGrids, ExtCtrls, DBCtrls, DB, DBTables, StdCtrls, DBEngine, BDE,
  24.   Menus;
  25.  
  26. type
  27.   TForm1 = class(TForm)
  28.     Table1: TTable;
  29.     MainMenu1: TMainMenu;
  30.     DataSource1: TDataSource;
  31.     DBGrid1: TDBGrid;
  32.     Exceptions1: TMenuItem;
  33.     KeyViolationPrimary1: TMenuItem;
  34.     KeyViolationSecondary1: TMenuItem;
  35.     FieldMissing1: TMenuItem;
  36.     NotinEditMode1: TMenuItem;
  37.     Table1CustNo: TFloatField;
  38.     Table1Company: TStringField;
  39.     Table1City: TStringField;
  40.     Database1: TDatabase;
  41.     InterBaseLoginFail1: TMenuItem;
  42.     procedure KeyViolationPrimary1Click(Sender: TObject);
  43.     procedure KeyViolationSecondary1Click(Sender: TObject);
  44.     procedure FieldMissing1Click(Sender: TObject);
  45.     procedure NotinEditMode1Click(Sender: TObject);
  46.     procedure InterBaseLoginFail1Click(Sender: TObject);
  47.   private
  48.     { Private declarations }
  49.   public
  50.     { Public declarations }
  51.   end;
  52.  
  53. var
  54.   Form1: TForm1;
  55.  
  56. implementation
  57.  
  58. {$R *.DFM}
  59.  
  60. //  Create a Primery key violation and display the message.
  61. procedure TForm1.KeyViolationPrimary1Click(Sender: TObject);
  62. var
  63.   Field1: integer;
  64.  
  65. begin
  66.   try
  67.     Table1.First;
  68.     Field1 := Table1.FieldByName('CustNo').AsInteger;
  69.     Table1.Insert;
  70.     Table1.FieldByName('CustNo').AsInteger := Field1;
  71.     Table1.FieldByName('Company').AsString := 'Not in table';
  72.     Table1.FieldByName('City').AsString := 'CA';
  73.     Table1.Post;
  74.   except
  75.     DBError.HandleException(Exception(ExceptObject));
  76.     Table1.Cancel;
  77.   end;
  78. end;
  79.  
  80. //  Create a Secondary key violation and display the message.
  81. procedure TForm1.KeyViolationSecondary1Click(Sender: TObject);
  82. var
  83.   Field2: string;
  84.  
  85. begin
  86.   try
  87.     Table1.First;
  88.     Field2 := Table1.FieldByName('Company').AsString;
  89.     Table1.Insert;
  90.     Table1.FieldByName('CustNo').AsInteger := 0;
  91.     Table1.FieldByName('Company').AsString := Field2;
  92.     Table1.FieldByName('City').AsString := 'CA';
  93.     Table1.Post;
  94.   except
  95.     DBError.HandleException(Exception(ExceptObject));
  96.     Table1.Cancel;
  97.   end;
  98. end;
  99.  
  100. //  Create a field is missing EDatabaseError and display the message.
  101. procedure TForm1.FieldMissing1Click(Sender: TObject);
  102. begin
  103.   try
  104.     Table1.Insert;
  105.     Table1.Post
  106.   except
  107.     DBError.HandleException(Exception(ExceptObject));
  108.     Table1.Cancel;
  109.   end;
  110. end;
  111.  
  112. //  Create a not in edit mode EDatabaseError and display the message.
  113. procedure TForm1.NotinEditMode1Click(Sender: TObject);
  114. begin
  115.   try
  116.     Table1.First;
  117.     Table1.FieldByName('CustNo').AsInteger := 100;
  118.   except
  119.     DBError.HandleException(Exception(ExceptObject));
  120.   end;
  121.  
  122. end;
  123.  
  124. procedure TForm1.InterBaseLoginFail1Click(Sender: TObject);
  125. begin
  126.   try
  127.     Database1.Open;
  128.   except
  129.     DBError.HandleException(Exception(ExceptObject));
  130.   end;
  131. end;
  132.  
  133. end.
  134.