Mega Code Archive

 
Categories / Delphi / ADO Database
 

Try..except rule for dataset edit mode

Title: try..except rule for dataset edit mode Question: How can we make sure a dataset doesn't get stuck in edit mode after a post has failed? Answer: { Dataset edit try except block example. by E.J.Molendijk (Delphi Factory Netherlands BV) How can we make sure a dataset doesn't get stuck in edit mode after a post has failed? This example shows how to use a try..except block to guard the programmatic edit. If something goes wrong during the edit or the post: The dataset is put back into normal mode and all modifications to the fields are cancelled. This code is usefull when transactions are not used in your application. This might be the case when using small database solutions such as Paradox. } // Go into edit mode MyDataset.Edit; try // Set the field(s) // .. you can add all the fields here ... MyDatset['Somefield'] := SomeValue; // attempt to post the data MyDataset.Post; except // something has gone wrong, cancel the edit MyDataset.Cancel; // pass on the exception to the next handler raise; end;