 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Eric Harmon Guest
|
Posted: Mon Feb 07, 2005 5:37 pm Post subject: Refreshing timestamp columns |
|
|
I have a Delphi app with a table named TEST structured like this:
ID integer,
Code varchar(12),
Description varchar(50),
TS timestamp
I want to refresh the timestamp when I update the record, so I have the
following code:
procedure TLocalDM.pvAfterUpdateRecord(Sender: TObject; SourceDS: TDataSet;
DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind);
var
ID: Integer;
begin
ID := 0;
if UpdateKind = ukInsert then begin
if VarIsNull(DeltaDS.FieldByName('ID').OldValue) or
(DeltaDS.FieldByName('ID').OldValue <= 0) then begin
DeltaDS.FieldByName('ID').ReadOnly := False;
ID := GetLastID;
DeltaDS.FieldByName('ID').NewValue := ID;
end;
end;
if ID = 0 then
ID := DeltaDS.FieldByName('ID').OldValue;
DeltaDS.FieldByName('TS').ReadOnly := False;
DeltaDS.FieldByName('TS').NewValue := GetTS(ID);
end;
GetTS looks like this:
function TLocalDM.GetTS(const TableName: string; ID: Integer): Variant;
begin
adsTS.Open;
try
Result := adsTSTS.AsVariant;
finally
adsTS.Close;
end;
end;
So the TS column should get updated in the dataset after posting (yes,
poPropogateChanges is set). The ID column works fine on Insert. For some
reason the TS field doesn't want to work. Can someone tell me what I'm
doing wrong?
Thanks in advance...
-Eric Harmon
|
|
| Back to top |
|
 |
Kostas Terzides Guest
|
Posted: Mon Feb 28, 2005 10:48 pm Post subject: Re: Refreshing timestamp columns |
|
|
Eric Harmon wrote:
| Quote: | I have a Delphi app with a table named TEST structured like this:
ID integer,
Code varchar(12),
Description varchar(50),
TS timestamp
I want to refresh the timestamp when I update the record, so I have the
following code:
procedure TLocalDM.pvAfterUpdateRecord(Sender: TObject; SourceDS: TDataSet;
DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind);
var
ID: Integer;
begin
ID := 0;
if UpdateKind = ukInsert then begin
if VarIsNull(DeltaDS.FieldByName('ID').OldValue) or
(DeltaDS.FieldByName('ID').OldValue <= 0) then begin
DeltaDS.FieldByName('ID').ReadOnly := False;
ID := GetLastID;
DeltaDS.FieldByName('ID').NewValue := ID;
end;
end;
if ID = 0 then
ID := DeltaDS.FieldByName('ID').OldValue;
DeltaDS.FieldByName('TS').ReadOnly := False;
DeltaDS.FieldByName('TS').NewValue := GetTS(ID);
end;
GetTS looks like this:
function TLocalDM.GetTS(const TableName: string; ID: Integer): Variant;
begin
adsTS.Open;
try
Result := adsTSTS.AsVariant;
finally
adsTS.Close;
end;
end;
So the TS column should get updated in the dataset after posting (yes,
poPropogateChanges is set). The ID column works fine on Insert. For some
reason the TS field doesn't want to work. Can someone tell me what I'm
doing wrong?
Thanks in advance...
-Eric Harmon
|
Usually if you want some changes you make to simultaneously update the
back end database and these changes to be sent back to client you use
BeforeUpdateRecord.
In your case, obviously (even if you don't clarify this) there is a
trigger in back end database giving a new generator value (or else ID
column updating wouldn't succeed either) and you retrieve that value
using GetLastID function. All you care about in AfterUpdateRecord is to
modify the values sent back to client.
If you want your GetTs function value to be applied to the back end
database then you should probably use either BeforeUpdateRecord event or
OnUpdateData (both are triggered before actual updating of data is done).
On the other hand, if your intention is to provide a timestamp value to
the backend database indicating succefull updating of data you should
manually use a query or stored procedure in AfterUpdateRecord as follows
(It is assumed that DeltaDs.FieldByName('ID).NewValue has been already
defined) :
var
V:Variant;
Qr:TSQLDataSet; //or TQuery or whatever you use
begin
Qr:=TSQLDataSet.Create(nil);
try
V:=Now;
DeltaDs.FieldByName('Ts').NewValue:=V;
Qr.CommandText:='Update "TableName" Set "Ts"='+V+' WHERE ID='+
DeltaDs.FieldByName('ID).NewValue;
Qr.ExecSQL(True);
finally
Qr.Free;
end;
The above code has a lot of errors (I wrote it in NotePad), but I
believe you got my point.
However, your adsTS component may be does what exactly I noted above. In
that case, more clarification of the parameters of your problem is
needed, so that someone here could find a way to help you. (after all
the code as provided in your example wouldn't compile at first place;
what is TableName parameter of GetTs function?)
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|