Recent

Author Topic: Operation cannot be performed on an active dataset  (Read 2865 times)

martinrame

  • Full Member
  • ***
  • Posts: 119
Operation cannot be performed on an active dataset
« on: June 22, 2018, 12:29:59 am »
Hi, I'm trying to do a Select, loop through its records, then call a procedure to update each record, but I'm getting "Operation cannot be performed on an active dataset". 

I wonder why this error, since my "UpdateRecords" procedure creates a new TSqlQuery instance.

Here's my code:

Code: Pascal  [Select][+][-]
  1. Procedure ListRecords;
  2. var                                                                                                                                            
  3.   lSql: TSqlQuery;        
  4.   id: Integer;                                                                                                                    
  5. begin                                                                                                                                          
  6.   lSql := TSqlQuery.Create(nil);                                                                                                                
  7.   try                                                                                                                                          
  8.     lSql.DataBase := DataModule1.PQConnection1;                                                                                              
  9.     lSql.SQL.Text := 'select * from table';                                                            
  10.     lSql.Open;                                                                                                                                  
  11.     while not lSql.Eof do                                                                                                                      
  12.     begin                            
  13.       id := lSql.FieldByName('id').AsInteger;                                                                                                                                                                                                              
  14.       UpdateRecord(id);                                                                                                    
  15.       lSql.Next;                                                                                                                                
  16.     end;                                                                                                                                        
  17.   finally                                                                                                                                      
  18.     lSql.Free;                                                                                                                                  
  19.   end;                                                                                                                                          
  20. end;              
  21.  
  22. Procedure UpdateRecords(Id: Integer);
  23. var                                                                                                                                            
  24.   lSql: TSqlQuery;                                                                                                                              
  25. begin                                                                                                                                          
  26.   lSql := TSqlQuery.Create(nil);                                                                                                                
  27.   try                                                                                                                                          
  28.     lSql.DataBase := DataModule1.PQConnection1;                                                                                              
  29.     lSql.Transaction := DataModule1.SqlTransaction1;                                                                                              
  30.     lSql.SQL.Text := 'update table set read=true where id=:id';
  31.     lSql.ParamByName('id').AsInteger := id;                                                            
  32.     lSql.ExecSql;
  33.     DataModule1.SqlTransaction1.Commit;                                                                                                                                  
  34.   finally                                                                                                                                      
  35.     lSql.Free;                                                                                                                                  
  36.   end;                                                                                                                                          
  37. end;
« Last Edit: June 22, 2018, 02:06:15 pm by martinrame »

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Operation cannI t be performed on an active dataset
« Reply #1 on: June 22, 2018, 05:16:10 am »
Why are you slowly updating all that on the client side instead of using the faster server side?

Code: SQL  [Select][+][-]
  1. UPDATE TABLE SET READ=TRUE
  2. WHERE id IN (SELECT id FROM TABLE WHERE condition = :condition);
« Last Edit: June 22, 2018, 05:34:23 am by valdir.marcos »

martinrame

  • Full Member
  • ***
  • Posts: 119
Re: Operation cannI t be performed on an active dataset
« Reply #2 on: June 22, 2018, 02:01:43 pm »
This is an example, not real code.

Inside my ListRecords function I do many more operations (that cannot be done in the database) than just updating records from the same table.

martinrame

  • Full Member
  • ***
  • Posts: 119
Re: Operation cannot be performed on an active dataset
« Reply #3 on: June 22, 2018, 02:11:03 pm »
Sorry, the error does not comes from this code, but from a nested query I'm doing against an SQLServer Database.

I'll try to isolate it, then come bak.

martinrame

  • Full Member
  • ***
  • Posts: 119
[SOLVED] Re: Operation cannot be performed on an active dataset
« Reply #4 on: June 22, 2018, 02:18:12 pm »
The solution was NOT commiting inside the nested procedure (the one tha does .ExecSQL), but commiting when the main loop ends.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Operation cannot be performed on an active dataset
« Reply #5 on: June 22, 2018, 02:19:59 pm »
My guess is that DataModule1.SqlTransaction1 is also connected to the query in ListRecords (via the database connection in DataModule1). Committing that transaction will also close the query in ListRecords.

You need to re-think the flow or set the correct parameter for the transaction that it will not close the queries automatically. Rethinking the flow is the better choice. Maybe use a separate transaction in UpdateRecords().

Edit: Ah, you already found it :)

 

TinyPortal © 2005-2018