After reading the Delphi help file about TDataSetProvider.OnUpdateData event explanation:
- Examine data (for example, for values or data changes that should not permitted), and raise exceptions that cancel applying of updates before they occur.
- Change data (for example encrypting or decrypting values) before it is sent on to the source dataset or database server.
I am looking for a sample code of how to change data for OnUpdateData. I have tried my best to look for solution. This is what I can achieve:
Example 1:
procedure TDBNextDocNo.DSPUpdateData(Sender: TObject; DataSet: TCustomClientDataSet);
begin
DataSet.First;
while not DataSet.EOF do begin
if DataSet.UpdateStatus = usUnmodified then begin
TPacketDataSet(Dataset).InitAltRecBuffers(True);
if DataSet.UpdateStatus in [usInserted, usModified] then begin
Dataset.Edit;
DataSet.FindField('MyField').AsString := 'zzz';
Dataset.Post;
end;
end;
end;
DataSet.Next;
end;
Problem for Example 1: Unfortunately, I keep receive error that some field value is missing. After perform some debug, I found that there are required fields that has empty value.
Example 2:
procedure TDBNextDocNo.DSPUpdateData(Sender: TObject; DataSet: TCustomClientDataSet);
begin
DataSet.First;
while not DataSet.EOF do begin
if DataSet.UpdateStatus = usUnmodified then begin
TPacketDataSet(Dataset).InitAltRecBuffers(True);
if DataSet.UpdateStatus in [usInserted, usModified] then
DataSet.FindField('MyField').NewValue:= 'zzz';
end;
DataSet.Next;
end;
end;
Problem for Example 2: By writing this way we no need to call DataSet.Edit & DataSet.Post. But the value 'zzz' which set to TField.NewValue is not being saved into database.
I have some special reason that the this update must perform in OnUpdateData instead of BeforeUpdateRecord/AfterUpdateRecord.
Please advice. Thank you very much.