home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / Runimage / Delphi50 / Demos / Db / Dberrors / dm1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  5.6 KB  |  182 lines

  1. (*
  2.   This example represents a sampling of the way that you might
  3.   approach trapping a number of database errors.
  4.  
  5.  
  6.   A complete listing of the database errorcodes is found in the
  7.   DBIErrs.Int file in the Delphi/Doc directory or in the IDAPI.h
  8.   file in the Borland Database Engine.
  9.  
  10.   Database errors are defined by category and code. Here's a sample:
  11.  
  12. { ERRCAT_INTEGRITY }
  13.  
  14.   ERRCODE_KEYVIOL               = 1;  { Key violation }
  15.   ERRCODE_MINVALERR             = 2;  { Min val check failed }
  16.   ERRCODE_MAXVALERR             = 3;  { Max val check failed }
  17.   ERRCODE_REQDERR               = 4;  { Field value required }
  18.   ERRCODE_FORIEGNKEYERR         = 5;  { Master record missing }
  19.   ERRCODE_DETAILRECORDSEXIST    = 6;  { Cannot MODIFY or DELETE this Master record }
  20.   ERRCODE_MASTERTBLLEVEL        = 7;  { Master Table Level is incorrect }
  21.   ERRCODE_LOOKUPTABLEERR        = 8;  { Field value out of lookup tbl range }
  22.   ERRCODE_LOOKUPTBLOPENERR      = 9;  { Lookup Table Open failed }
  23.   ERRCODE_DETAILTBLOPENERR      = 10; { 0x0a Detail Table Open failed }
  24.   ERRCODE_MASTERTBLOPENERR      = 11; { 0x0b Master Table Open failed }
  25.   ERRCODE_FIELDISBLANK          = 12; { 0x0c Field is blank }
  26.  
  27.  
  28.   The constant for the base category is added to these constants to represent
  29.   a unique DBI errorcode;
  30.  
  31.   DBIERR_KEYVIOL  = (ERRBASE_INTEGRITY + ERRCODE_KEYVIOL);
  32.   DBIERR_REQDERR = (ERRBASE_INTEGRITY + ERRCODE_REQDERR);
  33.   DBIERR_DETAILRECORDSEXIST = (ERRBASE_INTEGRITY + ERRCODE_DETAILRECORDSEXIST);
  34.   DBIERR_FORIEGNKEYERR = (ERRBASE_INTEGRITY + ERRCODE_FORIEGNKEYERR);
  35.  
  36.   The ERRBASE_INTEGRITY value is $2600 (Hex 2600) or 9728 decimal.
  37.   Thus, for example, the errorcode for keyviol is 9729
  38.                                    for master with details is 9734.
  39.  
  40.   *)
  41.  
  42. unit DM1;
  43.  
  44. interface
  45.  
  46. uses
  47.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  48.   DBTables, DB;
  49.  
  50. type
  51.   TDM = class(TDataModule)
  52.     Customer: TTable;
  53.     CustomerCustNo: TFloatField;
  54.     CustomerCompany: TStringField;
  55.     CustomerSource: TDataSource;
  56.     Orders: TTable;
  57.     OrdersSource: TDataSource;
  58.     Items: TTable;
  59.     ItemsOrderNo: TFloatField;
  60.     ItemsItemNo: TFloatField;
  61.     ItemsPartNo: TFloatField;
  62.     ItemsQty: TIntegerField;
  63.     ItemsDiscount: TFloatField;
  64.     ItemsSource: TDataSource;
  65.     OrdersOrderNo: TFloatField;
  66.     OrdersCustNo: TFloatField;
  67.     OrdersSaleDate: TDateTimeField;
  68.     OrdersShipDate: TDateTimeField;
  69.     OrdersEmpNo: TIntegerField;
  70.     procedure CustomerPostError(DataSet: TDataSet; E: EDatabaseError;
  71.       var Action: TDataAction);
  72.     procedure CustomerDeleteError(DataSet: TDataSet; E: EDatabaseError;
  73.       var Action: TDataAction);
  74.     procedure ItemsPostError(DataSet: TDataSet; E: EDatabaseError;
  75.       var Action: TDataAction);
  76.     procedure OrdersPostError(DataSet: TDataSet; E: EDatabaseError;
  77.       var Action: TDataAction);
  78.     procedure OrdersDeleteError(DataSet: TDataSet; E: EDatabaseError;
  79.       var Action: TDataAction);
  80.   private
  81.     { Private declarations }
  82.   public
  83.     { Public declarations }
  84.   end;
  85.  
  86. var
  87.   DM: TDM;
  88.  
  89. const
  90.   {Declare constants we're interested in}
  91.   eKeyViol = 9729;
  92.   eRequiredFieldMissing = 9732;
  93.   eForeignKey = 9733;
  94.   eDetailsExist = 9734;
  95.  
  96.  
  97. implementation
  98.  
  99. {$R *.DFM}
  100.  
  101. procedure TDM.CustomerPostError(DataSet: TDataSet;
  102.   E: EDatabaseError; var Action: TDataAction);
  103. begin
  104.   if (E is EDBEngineError) then
  105.     if (E as EDBEngineError).Errors[0].Errorcode = eKeyViol then
  106.     begin
  107.       MessageDlg('Unable to post: Duplicate Customer ID.', mtWarning, [mbOK], 0);
  108.       Abort;
  109.     end;
  110.  end;
  111.  
  112. procedure TDM.CustomerDeleteError(DataSet: TDataSet;
  113.   E: EDatabaseError; var Action: TDataAction);
  114. begin
  115.   if (E is EDBEngineError) then
  116.     if (E as EDBEngineError).Errors[0].Errorcode = eDetailsExist then
  117.     {the customer record has dependent details in the Orders table.}
  118.     begin
  119.       MessageDlg('To delete this record, first delete related orders and items.',
  120.         mtWarning, [mbOK], 0);
  121.       Abort;
  122.     end;
  123. end;
  124.  
  125. procedure TDM.ItemsPostError(DataSet: TDataSet; E: EDatabaseError;
  126.   var Action: TDataAction);
  127. begin
  128.   {This error will occur when a part number is specified that
  129.    is not in the parts table.}
  130.   if (E as EDBEngineError).Errors[0].Errorcode = eForeignKey then
  131.   begin
  132.     MessageDlg('Part number is invalid', mtWarning,[mbOK],0);
  133.     Abort;
  134.   end;
  135. end;
  136.  
  137. procedure TDM.OrdersPostError(DataSet: TDataSet; E: EDatabaseError;
  138.   var Action: TDataAction);
  139. var
  140.   iDBIError: Integer;
  141. begin
  142.   if (E is EDBEngineError) then
  143.   begin
  144.     iDBIError := (E as EDBEngineError).Errors[0].Errorcode;
  145.     case iDBIError of
  146.       eRequiredFieldMissing:
  147.         {The EmpNo field is defined as being required.}
  148.         begin
  149.           MessageDlg('Please provide an Employee ID', mtWarning, [mbOK], 0);
  150.           Abort;
  151.         end;
  152.       eKeyViol:
  153.         {The primary key is OrderNo}
  154.         begin
  155.           MessageDlg('Unable to post. Duplicate Order Number', mtWarning,
  156.             [mbOK], 0);
  157.           Abort;
  158.         end;
  159.     end;
  160.   end;
  161. end;
  162.  
  163. procedure TDM.OrdersDeleteError(DataSet: TDataSet; E: EDatabaseError;
  164.   var Action: TDataAction);
  165. begin
  166.   if E is EDBEngineError then
  167.     if (E as EDBEngineError).Errors[0].Errorcode = eDetailsExist then
  168.     begin
  169.       if MessageDlg('Delete this order and related items?', mtConfirmation,
  170.         [mbYes, mbNo], 0) = mrYes then
  171.       begin
  172.         {Delete related records in linked 'items' table}
  173.         while Items.RecordCount > 0 do
  174.           Items.delete;
  175.         {Finally,delete this record}
  176.         Action := daRetry;
  177.       end else Abort;
  178.     end;
  179. end;
  180.  
  181. end.
  182.