So I have a ClientDataset (cdsM1) with a Nested Detail (cdsD1). I need to print it before do ApplyUpdates, so I clone them (cdsMclone and cdsDclone) and filter the master clone just to show only one master record.
After printing, I need to update the record. At first I tried something like this:
cdsMclone.Edit;
cdsMclone.FieldByName('DATEPRINTED').AsString := Now;
cdsMclone.Post;
cdsMclone.ApplyUpdates(0);
But after this, if I change anything more in the source clientdataset, cdsM1.ApplyUpdates(0)
generates a conflict (and by this I mean one that you should respond in a OnReconcileError
). But this one should not be showed to the user. Also, using this approach I am sending at least two requests to the Database.
One workaround is using CloneSource
property. Example:
cdsMclone.CloneSource.Edit;
cdsMclone.CloneSource.FieldByName('DATEWASPRINTED').AsString := Now;
cdsMclone.CloneSource.Post;
This way, I can call cdsM1.ApplyUpdates(0)
without worries. But I really don't like this. Seems the code is changing something it should not do directly. Also, how would the code looks like if in the future I need to change something in the nested detail?
There is any other way to return back changes from the cloned clientdataset to the source?
If your clones are created using
CloneCursor()
then the original and the clone share the same underlying dataset. They are not separate sets of data, only two different views into the same data.You do not provide details of the filter used to restrict your original data set to 1 record. However, I suspect that your change to the record is conflicting with the conditions of that filter leading to the conflict error you are seeing.
Using the clone avoids this because the cloned dataset does not have this filter applied. It is a separate view with its own filters. Hence there is no conflict.
There is no issue with modifying the underlying dataset through the cloned cursor in this way. As stated, the clone and the clone source are operating over the same data set, so this would appear to achieve precisely what you wish.
It is an aspect of your application that would benefit from some documentation however, to make the intention and dependencies involved more immediately apparent in the code for those who maintain that code in months and years to come.