Page 1 of 1

Bound Controls

Posted: Sat 04 Jul 2009 13:19
by jtwatssi
The following code is in the MyDAC help file and illustrates the use of cached updates and transactions in response to a button click.

procedure ApplyButtonClick(Sender: TObject);
begin
with MyQuery do
begin
Session.StartTransaction;
try
... {Modify data}
ApplyUpdates; {try to write the updates to the database}
Session.Commit; {on success, commit the changes}
except
RestoreUpdates; {restore update result for applied records}
Session.Rollback; {on failure, undo the changes}
raise; {raise the exception to prevent a call to CommitUpdates!}
end;
CommitUpdates; {on success, clear the cache}
end;
end;

This would be just fine if you put the code that writes the field values in the first part of the try except block. But that's if you're manually coding the field value changes and using unbound controls.

What I don't quite understand though is how you would do this using bound controls. As soon as the user begins typing in a bound control the dataset is put into Edit mode if its not already. Would you start the transaction in the BeforeEdit and BeforeInsert events and then commit after the button click or maybe in the dataset's AfterPost event?

I've never really seen many good examples of the use of cached updates and transactions utilizing the dataset events on a form with bound controls.

What am I missing here? Would it be better to use unbound controls?

Thanks.

JTW

Posted: Mon 06 Jul 2009 13:38
by Plash
When a user edits a record and posts it, the changes are not posted to the database if CachedUpdates = True. The changes are stored in local buffer.

When a user presses the button, you can execute the code from this example (without {Modify data}) to save the changes to the database.