Recent

Author Topic: operation cannot be performed on an inactive dataset.  (Read 3429 times)

marcm

  • New member
  • *
  • Posts: 7
operation cannot be performed on an inactive dataset.
« on: March 27, 2021, 08:48:49 pm »
Hello everyone!
I have a form with some Dbedt. and when I start the form I wanted the program to check if the ID of this Dbedt exists in the corresponding table. If this data exists there, the table will be opened and the data placed in the subsequent Dbedts.

However, it turns out that when running the application and showing the form it gives the error message below:
operation cannot be performed on an inactive dataset.

Follow the code used.

procedure TfrmS21teste . FormShow ( Sender : TObject );
begin
    if dbeditId.text <> '' then
        dm.tbSetembro.Close;
        dm.tbSetembro.SQL.Clear;
        dm.tbSetembro.SQL.Text:= 'SELECT * FROM setembro WHERE idCliente = frmS21teste.dbeditId.Text';
    if dm.tbSetembro.RecordCount >= 1 then
      dm.tbSetembro.Open;

end;

Sorry for the bad English,

tanks
Marcm
« Last Edit: March 27, 2021, 08:51:48 pm by marcm »

speter

  • Sr. Member
  • ****
  • Posts: 359
Re: operation cannot be performed on an inactive dataset.
« Reply #1 on: March 28, 2021, 12:31:53 am »
Your indenting seems confusing:
Code: Pascal  [Select][+][-]
  1. procedure TfrmS21teste . FormShow ( Sender : TObject );
  2. begin
  3.     if dbeditId.text <> '' then
  4.         dm.tbSetembro.Close;
  5.  
  6.     dm.tbSetembro.SQL.Clear;
  7.     dm.tbSetembro.SQL.Text:= 'SELECT * FROM setembro WHERE idCliente = frmS21teste.dbeditId.Text';
  8.  
  9.     if dm.tbSetembro.RecordCount >= 1 then
  10.       dm.tbSetembro.Open;
  11. end;

did you mean:
Code: Pascal  [Select][+][-]
  1. procedure TfrmS21teste . FormShow ( Sender : TObject );
  2. begin
  3.     if dbeditId.text <> '' then
  4.       begin
  5.         dm.tbSetembro.Close;
  6.         dm.tbSetembro.SQL.Clear;
  7.         dm.tbSetembro.SQL.Text:= 'SELECT * FROM setembro WHERE idCliente = frmS21teste.dbeditId.Text';
  8.       end;
  9.  
  10.     if dm.tbSetembro.RecordCount >= 1 then
  11.       dm.tbSetembro.Open;
  12. end;

also, if " frmS21teste.dbeditId.Text" is a variable in your program (not a field in the db) I think that line should be:
Code: Pascal  [Select][+][-]
  1. dm.tbSetembro.SQL.Text:= 'SELECT * FROM setembro WHERE idCliente = '+frmS21teste.dbeditId.Text;
assuming idCliente is a number in the db (if it is a text field you need to enclose it in double quotes).

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

paweld

  • Hero Member
  • *****
  • Posts: 1293
Re: operation cannot be performed on an inactive dataset.
« Reply #2 on: March 28, 2021, 07:12:44 am »
you are trying to read the RecordCount before open the query.
Code: Pascal  [Select][+][-]
  1. procedure TfrmS21teste . FormShow ( Sender : TObject );
  2. begin
  3.   if dbeditId.Text = '' then
  4.     exit;
  5.   if dm.tbSetembro.Active then //close query if active
  6.     dm.tbSetembro.Close;
  7.   //dm.tbSetembro.SQL.Clear;  //if you set Sql.Text do no need SQL.Clear
  8.   dm.tbSetembro.SQL.Text:= 'SELECT * FROM setembro WHERE idCliente = :id';
  9.   dm.tbSetembro.ParamByName('id').AsString := frmS21teste.dbeditId.Text;
  10.   dm.tbSetembro.Open;
  11.   if dm.tbSetembro.RecordCount = 0 then  //RecordCount is available only when query is Active
  12.     dm.tbSetembro.Close;
  13. end;
« Last Edit: March 28, 2021, 07:18:16 am by paweld »
Best regards / Pozdrawiam
paweld

marcm

  • New member
  • *
  • Posts: 7
Re: operation cannot be performed on an inactive dataset.
« Reply #3 on: March 28, 2021, 04:53:19 pm »
Hi,
thanks for reply!

now the error change.

SQL Error: database is locked

I want to do a search on the table and if it contains data I want it to display in the DBEdits, but if there is no data in the table, it opens and let me enter the values ​​that I type in the DBedits.

Here is the code that displays the error:

procedure TfrmS21teste . FormShow ( Sender : TObject );
begin
    if dbeditId.text <> '' then
       if dm.tbSetembro.Active then //close query if active
        //dm.tbSetembro.Close;
        //dm.tbSetembro.SQL.Clear;
        dm.tbSetembro.SQL.Text:= 'SELECT * FROM setembro WHERE idCliente = :id';
        dm.tbSetembro.ParamByName('id').AsString := frmS21teste.dbeditId.Text;
        dm.tbSetembro.Open;
    if dm.tbSetembro.RecordCount >= 1 then
      dm.tbSetembro.Open
end;





paweld

  • Hero Member
  • *****
  • Posts: 1293
Re: operation cannot be performed on an inactive dataset.
« Reply #4 on: March 29, 2021, 07:44:41 am »
if you want to search for a record in an open table use Locate function, eg.
Code: Pascal  [Select][+][-]
  1. var
  2.   id: Integer;
  3. begin
  4.   if TryStrToInt(editId.Text, id) then
  5.   begin
  6.     if not tbSeptembre.Locate('idCliente', id, []) then
  7.       ShowMessage('idCliente not found!');
  8.   end
  9.   else
  10.     ShowMessage(editId.Text + ' is not a number');  
Best regards / Pozdrawiam
paweld

egsuh

  • Hero Member
  • *****
  • Posts: 1533
Re: operation cannot be performed on an inactive dataset.
« Reply #5 on: March 29, 2021, 01:45:58 pm »
You cannot do anything on closed (inactive) dataset. First you have to open it and then do whatever you want.
I think you'd better use TEdit,  instead of TDBEdit --- DBEdit is linked to a field, and displays (and edits) the content of the field automatically.


Code: Pascal  [Select][+][-]
  1.         dm.tbSetembro.Close;
  2.         dm.tbSetembro.SQL.Text:= 'SELECT * FROM setembro WHERE idCliente = :id';
  3.         dm.tbSetembro.ParamByName('id').AsString := frmS21teste.dbeditId.Text;
  4.         dm.tbSetembro.Open;
  5.  
  6.         if dm.tbSetembro.IsEmpty then dm.tbSetembro.Close
  7.         else begin
  8.                 // do whatever you want
  9.         end;
  10.  
     

marcm

  • New member
  • *
  • Posts: 7
Re: operation cannot be performed on an inactive dataset.
« Reply #6 on: March 29, 2021, 11:13:07 pm »
tks egsuh

I managed to make it work with the code you indicated.
Now he is doing what I wanted, putting the data in the Dbedits of the corresponding months.
I will implement the other months now and see if everything is right.

Thank you again!
 :D

Can Close the Post


  dm.tbSetembro.Close;
  dm.tbSetembro.SQL.Text:= 'SELECT * FROM setembro WHERE idCliente = :id';
  dm.tbSetembro.ParamByName('id').AsString := frmS21teste.dbeditId.Text;
  dm.tbSetembro.Open;

  dm.tbOutubro.Close;
  dm.tbOutubro.SQL.Text := 'SELECT * FROM outubro WHERE idCliente = :id' ;
  dm.tbOutubro.ParamByName('id').AsString := frmS21teste.dbeditId.Text;
  dm.tbOutubro.Open;

 

TinyPortal © 2005-2018