home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 April / Chip_1997-04_cd.bin / prezent / cb / data.z / ERRFORM.CPP < prev    next >
C/C++ Source or Header  |  1997-01-16  |  5KB  |  119 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++ Builder
  3. //Copyright (c) 1987 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl\vcl.h>
  7. #pragma hdrstop
  8.  
  9. #include "errform.h"
  10. //---------------------------------------------------------------------------
  11. #pragma resource "*.dfm"
  12. TUpdateErrorForm *UpdateErrorForm;
  13. //---------------------------------------------------------------------------
  14. __fastcall TUpdateErrorForm::TUpdateErrorForm(TComponent* Owner)
  15.   : TForm(Owner)
  16. {
  17. }
  18. //---------------------------------------------------------------------------
  19. // This method handles the error by displaying a dialog with information about
  20. //  the error and then allowing the user to decide what course of action to take
  21.  
  22. TUpdateAction __fastcall TUpdateErrorForm::HandleError(TDataSet *DataSet,
  23.             EDatabaseError *E, TUpdateKind UpdateKind)
  24. {
  25.    AnsiString UpdateKindStr[] = {"Modified", "Inserted", "Deleted"};
  26.  
  27.    // Put the error context information into the labels on the form
  28.    UpdateType->Caption = UpdateKindStr[UpdateKind];
  29.    ErrorText->Caption = E->Message;
  30.    // Fill the string grid with the update field values
  31.    GetFieldValues(DataSet);
  32.    ShowModal();
  33.    switch (ModalResult)
  34.    {
  35.       case mrRetry:
  36.       {
  37.           // If user wants to retry, then put any changed values from the
  38.           //  string grid back into the associated field's NewValue property
  39.           SetFieldValues(DataSet);
  40.           return uaRetry;
  41.       }
  42.       case mrIgnore:
  43.          return uaSkip;
  44.       default:
  45.          return uaAbort;
  46.    }
  47. }
  48. //---------------------------------------------------------------------------
  49. void __fastcall TUpdateErrorForm::GetFieldValues(TDataSet *DataSet)
  50. {
  51.    int      I;
  52.    TField   *F;
  53.  
  54.    // Create a list of the data fields in the dataset, and store them in
  55.    //  a stringlist which we can use to determine which values the user
  56.    //  has edited
  57.  
  58.    FDataFields->Clear();
  59.    for (I = 0; I <= DataSet->FieldCount - 1; I++)
  60.       if (DataSet->Fields[I]->FieldKind == fkData)
  61.          FDataFields->AddObject("", DataSet->Fields[I]);
  62.  
  63.    // Now fill up the string grid with the Old and New values of each field.
  64.    // OldValue and NewValue are public properties of TDataSet which are used
  65.    // from within the OnUpdateError event handler to determine what data a
  66.    // user has updated.  We use the VarToStr RTL function to ensure that null
  67.    // fields are displayed as blank strings
  68.  
  69.    UpdateData->RowCount = FDataFields->Count + 1;
  70.    for (I = 0; I <= FDataFields->Count - 1; I++)
  71.    {
  72.       F = (TField*)FDataFields->Objects[I];
  73.       UpdateData->Cells[0][I + 1] = VarToStr(F->NewValue);
  74.       UpdateData->Cells[1][I + 1] = VarToStr(F->OldValue);
  75.    }
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TUpdateErrorForm::SetFieldValues(TDataSet *DataSet)
  79. {
  80.    int      I;
  81.    TField   *F;
  82.  
  83.    for (I = 0; I <= DataSet->FieldCount - 1; I++)
  84.    // We set the string in the data field list to '*' for any fields the
  85.    //   user edited in the string grid.  Those are the only fields we need
  86.    //   to write back into the associated TField's NewValue property
  87.       if (CompareText(FDataFields->Strings[I], "*") == 0)
  88.       {
  89.          F = (TField*)FDataFields->Objects[I];
  90.          F->NewValue = UpdateData->Cells[0][I + 1];
  91.       }
  92. }
  93. // Event handlers
  94. //---------------------------------------------------------------------
  95. void __fastcall TUpdateErrorForm::FormCreate(TObject *Sender)
  96. {
  97.    FDataFields = new TStringList();
  98.    // Fill in the titles of the string grid
  99.    UpdateData->Cells[0][0] = "New Value";
  100.    UpdateData->Cells[1][0] = "Old Value";
  101.  
  102. }
  103. //---------------------------------------------------------------------
  104. void __fastcall TUpdateErrorForm::FormDestroy(TObject *Sender)
  105. {
  106.    delete FDataFields;
  107. }
  108. //---------------------------------------------------------------------
  109. void __fastcall TUpdateErrorForm::UpdateDataSetEditText(TObject *Sender,
  110.       long ACol, long ARow, const AnsiString Value)
  111. {
  112.    // Set a flag in the list of datafields indicating that this value
  113.    // was changed.
  114.    FDataFields->Strings[ARow - 1] = "*";
  115.  
  116. }
  117. //---------------------------------------------------------------------
  118.  
  119.