I'm stuck with the following problem : I would like to be able to disconnect a dataset from the database but be able to modify it locally (and I don't need to propagate modifications back to DB).
I'm stuck with the following problem : I would like to be able to disconnect a dataset from the database but be able to modify it locally (and I don't need to propagate modifications back to DB).
This sounds like TClientDataset Briefcase model but I can't get it to work . Any idea why ?
Var
Qu : TAdsQuery;
cdsData : TClientDataSet;
DataSetProvider: TDataSetProvider;
Begin
cdsData := TClientDataSet.Create(self);
Qu := TAdsQuery.Create(self);
Qu.AdsConnection := AdsConnection1;
Qu.SQL.Add('SELECT * FROM MYTABLE WHERE CRITERIA='something');
Qu.Open;
DataSetProvider := TDataSetProvider.Create(Nil);
DataSetProvider.DataSet := Qu;
cdsData.SetProvider(DataSetProvider);
cdsData.Open;
Qu.Close;
cdsData.First;
cdsData.Edit;
i := 1;
While Not cdsData.eof Do
Begin
If i Mod 2 = 0 Then // modify only even rows , for test
cdsData.FieldByName('ID_PARAMETRAGE').AsInteger := 0; // EDatabase error here : not in edit or insert mode
Inc(i);
cdsData.Next;
End;
cdsData.Close;
cdsData.Free;
Qu.Free
This sounds like TClientDataset Briefcase model but I can't get it to work . Any idea why ?
Var
Qu : TAdsQuery;
cdsData : TClientDataSet;
DataSetProvider: TDataSetProvider;
Begin
cdsData := TClientDataSet.Create(self);
Qu := TAdsQuery.Create(self);
Qu.AdsConnection := AdsConnection1;
Qu.SQL.Add('SELECT * FROM MYTABLE WHERE CRITERIA='something');
Qu.Open;
DataSetProvider := TDataSetProvider.Create(Nil);
DataSetProvider.DataSet := Qu;
cdsData.SetProvider(DataSetProvider);
cdsData.Open;
Qu.Close;
cdsData.First;
cdsData.Edit;
i := 1;
While Not cdsData.eof Do
Begin
If i Mod 2 = 0 Then // modify only even rows , for test
cdsData.FieldByName('ID_PARAMETRAGE').AsInteger := 0; // EDatabase error here : not in edit or insert mode
Inc(i);
cdsData.Next;
End;
cdsData.Close;
cdsData.Free;
Qu.Free
As the error says, you need to be in edit mode to edit the dataset. cdsData.Edit; ... cdsData.Post;
ReplyDeleteI have a cdsData.Edit .... Just after cdsData.First;
ReplyDeleteOh OK , got it ! thanks Ondrej Kelle !!! I need to put Edit and post around my update.
ReplyDeletePut it inside the loop. Navigating away from the current record (Next) cancels or auto-posts the edit and puts the dataset back in browse mode.
ReplyDeleteCorrected version :
ReplyDeleteVar
Qu : TAdsQuery;
cdsData : TClientDataSet;
DataSetProvider: TDataSetProvider;
Begin
cdsData := TClientDataSet.Create(self);
Qu := TAdsQuery.Create(self);
Qu.AdsConnection := AdsConnection1;
Qu.SQL.Add('SELECT * FROM MYTABLE WHERE CRITERIA='something');
Qu.Open;
DataSetProvider := TDataSetProvider.Create(Nil);
DataSetProvider.DataSet := Qu;
cdsData.SetProvider(DataSetProvider);
cdsData.Open;
Qu.Close;
cdsData.First;
i := 1;
While Not cdsData.eof Do
Begin
If i Mod 2 = 0 Then // modify only even rows , for test
begin
cdsData.Edit();
cdsData.FieldByName('ID_PARAMETRAGE').AsInteger := 0; /// EDatabase error here : not in edit or insert mode
cdsData.Post();
end;
Inc(i);
cdsData.Next;
End;
cdsData.Close;
cdsData.Free;
Qu.Free