Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TClientDataSet.ApplyUpdates() doesn't apply updates

My Delphi project has a TAdoQuery accesssing data on an MS Sql Server 2014 server, and TClientDataSet that receives the AdoQuery data via a TDataSetProvider. This is created from a Project Template I set up.

Normally, I've found this set-up to work faultlessly, but with this particular project I'm having a problem: ApplyUpdates() fails silently and the Sql Server data is not updated. In my stripped down debugging project, the only code I have, apart from a button-click handler which calls it, is:

procedure TForm1.ApplyUpdates;
var
  Errors : Integer;
begin
  Errors := ClientDataSet1.ApplyUpdates(0);
  Caption := IntToStr(Errors) + '/' + IntToStr(ClientDataSet1.ChangeCount);
end;

After this executes, the form's caption should be 0/0 of course but what it actually says is 0/1. So on the face of it, no errors occurred but the CDSs ChangeCount hasn't been reset to zero as it should be. My q is, how can ApplyUpdates return no errors but the server dataset doesn't get updated.

Fwiw, I added the ChangeCount display as part of my effort to debug the problem. But I'm afraid I haven't been able to follow what's supposed to be going on in the details of the "conversation" between the DataSetProvider and its DataSet to apply the updates on the server.

like image 427
Alex James Avatar asked Jul 03 '16 16:07

Alex James


1 Answers

I ran into this problem recently on a quick project I rustled up without the precaution of setting an OnReconcileError handler, as queried by @mjn.

Once I'd set up the OnReconcileError handler, it was obvious that the problem was that the provider's TSqlResolver wasn't able to identify the row to update. Iirc, the message on the ReconcileError pop-up form was words to the effect of "Unable to locate record. No key specified."

So, first thing I tried was to include this in my CDS's AfterOpen:

CDS1.Fields[0].ProviderFlags := [pfInKey];

(CDS1.Fields[0] is the PK field of the dataset)

Contrary to my expectations, that didn't fix it. After scratching my head for a while, I had a careful look on the server and discovered that the recently-recreated table I was using did not have a primary key index.

Once I'd created the primary key index on the server, the ApplyUpdates problem went away.

However, what puzzles me about this is that prompted by your q, I dropped the primary key index on my server table and the problem hasn't started occurring again (!). I'm guessing this is due to some kind of cacheing effect on my machine but I don't really want to reboot it right now to investigate.

like image 198
MartynA Avatar answered Nov 12 '22 11:11

MartynA