Consider there is a clientdataset which is created for one of the table in the database.

Select GuestNo, ReportDate, SeqNo, GuestDetails from Guest.

[Here, GuestNo, ReportDate and SeqNo have the Primary keys]

I'm adding multiple records to the clientdataset,

cds->FieldByName("GuestNo")->AsInteger = 100;
cds->FieldByName("ReportDate")->AsDateTime = "2023-12-19";
cds->FieldByName("SeqNo")->AsInteger = 1;

cds->FieldByName("GuestNo")->AsInteger = 100;
cds->FieldByName("ReportDate")->AsDateTime = "2023-12-19";
cds->FieldByName("SeqNo")->AsInteger = 2;

cds->FieldByName("GuestNo")->AsInteger = 100;
cds->FieldByName("ReportDate")->AsDateTime = "2023-12-19";
cds->FieldByName("SeqNo")->AsInteger = 3;

but before posting the data to the database, I'm trying to update the ReportDate field in the clientdataset for all the records, where the field to be updated has a primary key in that table.

cds->First();
while (!cds->Eof)
{
    cds->Edit();
    cds->FieldByName("ReportDate")->AsDateTime = "2023-12-20";
    cds->Next();
}

In this case, when iterating through the records, after updating the field for the first record and then calling Next() doesn't navigate to the next record, and it ends the loop after the first iteration itself. I assume that, since the field I'm updating has the primary key, after the updating the first record, it looks up for the next record for the updated new date and therefore cds->Eof becomes true, since there is no record for the new date and it breaks out of the loop.

This is happening only when trying to update any of the field which is having primary key in that table but the other fields are updating fine.

Is there a way to change the value of the primary key field in the dataset, which has multiple records ?

I have tried by calling DisablingConstraints() for the dataset, but that doesn't work too.

0

There are 0 best solutions below