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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating Tclientdataset in it's eof 1

Status
Not open for further replies.

Karen99

Programmer
Aug 5, 2003
113
ZA
Is the following code possible :

with Tclientdataset do
begin
close;
CommandText := ('select * from a);

Open;
First;

while not Eof do
begin
inc(counter);
Tclientdataset.edit;
Tclientdataset.fieldbyname('counter').value :=
counter;
Tclientdataset.applyupdates(-1);
next;
end;
end;

Please don't go into the nitty-gritty detail of this code, this is just a simplified version of the code I have. I just tried to get the point across that is it possible to, while you are looping through a clientset, to update the record that you are standing on. The TClientset is connected to a provider and query.

I am using D7 and DBExpress.
 
The answer is yes you can update the records in a ClientDataset as you loop through it.

You say don't go into the nitty gritty detail of the code but you are missing a vital statement that will stop it from working. Maybe your simplification of the code has removed it?

Andrew
 
Delphi has always (since version 1!) automatically posted a changed record when you move off of that record.

I had a hard time with this when I first started with Delphi because, comming from a Clipper background, I was used to editing a record and then searching through the same table prior to posting the edit to see if the edited record was a duplicate.

-Dell
 
Ok, Towerbase what am I missing ?

And Hilfy, does that mean I don't have to applyupdates after I edited it ?

Karen
 
I think your code should look more like:
Code:
with Tclientdataset do begin
 Close;
 CommandText := 'SELECT * FROM a';
 Open;
 First;

 while not Eof do begin
  Inc(counter);
  Edit;
  Fieldbyname('counter').AsInteger := counter;
  Post;
  Next;
 end;

 if ApplyUpdates(0) > 0 then
   ShowMessage ('Unable to update Counter');
end;

For performance reasons, the ApplyUpdates is probably best done outside the loop. I haven't tested this to determine the improvement - a lot will depend on individual circumstances. I think you should be using zero as the parameter to ApplyUpdates to indicate that you won't tolerate any errors in the updating process. But there may be circumstances when zero is not appropriate. At least you should indicate if ApplyUpdates has worked or not.

I believe that 'AsInteger' is quicker than using 'Value'.

Dell is correct to say that Post is not necessary if you move off a changed record. However, Borland do include Post in sample code when in a similar loop (for example, Delphi 7 Developers Guide page 30-9). I think that it is a good habit to explicitly code Post because in some circumstances you might lose an update (for example, if you call Close without moving off the record).

Karen, presumably you posted your original message because there was a problem with doing the update?

Andrew
 
Thanks Andrew !!

As you can see, I am new to DBExpess and am still trying to figure it out.

When do you use SQLDataSet and when do you use ClientDataset with a Provider and dbexpress query? What is the difference ?

And why link a clientdataset to a provider to a dbexpress query if you can type in the sql directly into commandtext of clientdataset ?

And if you have clientdataset and you want to insert a record. Will the commandtext be : 'insert into A ....' or will it be 'select * from a' and then you have :

clientdataset.insertrecord();
clientdataset.post;

I am also trying to read about this, but it sometimes feel the author of some books assume that you know a few fundamental principles. And I don't ....

thanks
karen
 
And :

When do you use a source ? Is that only when you want to link the dataset to eg. a grid or combobox or are there another reason ?

Thanks again
Karen
 
ok I am confuse. Why does this not work :

with clientdataset do
begin
commandtext := ('select * from c');
open;
insert;
fieldbyname('a').asinteger := 0;
fieldbyname('b').asinteger := 7;
post;
end;

and this does :

with clientdataset do
begin
commandtext := 'insert into c (a,b) values (0,7)';
execute;
end;

And where does insertrecord comes in, because that does not seem to work either ?
 
Karen,

Too many questions for this thread. I've just written several FAQs relating to dbExpress and I hope they answer your questions. If they do not then can you raise a new thread for each question?

Andrew
 
Thanks Andrew. I have just read you FAQs. I think I am getting the general idea. Will try it out tommorow.

Karen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top