Link to home
Start Free TrialLog in
Avatar of syloux
syloux

asked on

Working with StartTransaction ???


How can I use correctly StartTransaction ?

I have this :
( Query1.CachedUpdate := True )

  Database1.Open ;
  Database1.StartTransaction ;

  With Query1, Sql Do
    Begin
      Close ;
      Clear ;
      Add('INSERT INTO Test') ;
      Add('VALUES (:Username, :LongName)') ;
      ParamByName('Username').AsString := 'Toto' ;
      ParamByName('Longname').AsString := 'Grand Toto' ;
      ExecSql ;
    End ;

  Query1.ApplyUpdates ;
  Database1.Commit ;    
  Query1.CommitUpdates ;

But when I run it, I have and error messsage on Query1.ApplyUpdates :
"Dataset not in edit or insert mode."

What can I do ?
Thanks

ASKER CERTIFIED SOLUTION
Avatar of excalibur256
excalibur256
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of geobul
geobul

Hi,
I'm not sure but try this:

Database1.Open ;

  With Query1, Sql Do
  Begin
    Close ;
    Clear ;
    Add('INSERT INTO Test') ;
    Add('VALUES (:Username, :LongName)') ;
    ParamByName('Username').AsString := 'Toto' ;
    ParamByName('Longname').AsString := 'Grand Toto' ;
    ExecSql ;
  end;

  if Query1.UpdatesPending then
    Database1.ApplyUpdates([Query1]);
End ;

Regards, Geo
Avatar of kretzschmar
i guess cached updates are only usefull with an select-query,

you get the error because you don't call query.edit or query.insert,
which may cause a dataset not open error, because you can't open a insert query

if you want the capability of cached updates, then use a select-query,
open the query,
insert your new records with the insert method,
supply your fieldvalues with query.fielsbyname().as.. := whatever
post the insert and
start again with a new record,
if you are ready then you do a applyupdate

meikl ;-)
Hi,
Using my solution you don't need to think what the query does (select, insert, etc.) because you check for pending updates first.
The second thing is that TDatabase.ApplyUpdates does all the transaction stuff internally (starts a transaction, commits or rolls back and clears the updates cache) for you.

Regards, Geo