Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to update data in a TClientDataset?

dbExpress

How to update data in a TClientDataset?

by  towerbase  Posted    (Edited  )
You can traverse a TClientDataset using First, Next, Last, Prior, FindKey, FindNearest and other functions/procedures. When you want to update data in a record you can explicitly put the record into Edit mode, change the data and Post it as in this example where the TClientDataset has been instantiated with the name of cds.
Code:
  cds.Edit;
  cds.FieldByName('credit').AsInteger := 500;
  cds.Post;
At this point the data has been added to a change log in the TClientDataset but it has not been updated in the physical database. The change log enables updates to be undone quite easily.

Alternatively, you can update the data using standard visual Data Access controls such as TDBEdit, TDBGrid and so
on. Again, the data is only changed within TClientDataset and not in the real database.

You must call ApplyUpdates to actually send the updates to the real database:
Code:
  cds.ApplyUpdates(0);
ApplyUpdates is actually a function that returns the number of updates that failed. Failure could be caused by someone else updating the record. The parameter passed to ApplyUpdates specifies the number of updates that you will permit to fail. If this number is exceeded all the updates are backed out. If the number of updates is specified as -1 then it indicates that you will tolerate any number of failures. A better way of coding the ApplyUpdate might be:
Code:
  if cds.ApplyUpdates(0) > 0 then
    ShowMessage ('Update failed');
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top