user863551
user863551

Reputation:

Handling Tables, preventing dataset errors

I'm creating an application that requires me to post a lot of data into tables as well as changing values within the table.

Whenever I write applications most of my time seems to be stopping errors such as:

'Cannot perform this method on a closed dataset'`

or

'tblName not in edit or insert mode'`

I seemingly get these every time and they aren't easy to debug and find out what's going wrong where.

I'm just looking for some guidance on how I can stop this from happening, I know it's something I am doing. For example, at the moment I am getting:

'Cannot perform this operation on a closed dataset'

somewhere within this code:

procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if not tblSystem.Active then
    tblSystem.Open;

  if not tblSpecs.Active then
    tblSpecs.Open;

  tblSpecs.Edit;
  tblSpecs.Post;
  tblSpecs.Close;

  // Post changes to the notes on exit
  tblSystem.Edit;
  tblsystem.Post;
  tblSystem.Close;
end;

and

procedure TfrmMain.chkAutoUpdateClick(Sender: TObject);
begin
  tblSystem.Edit;
  if chkAutoUpdate.Checked then
    chkInstUpdates.Enabled := True
  else
  begin
    chkInstUpdates.Enabled := False;
    tblSystemAutoUpdate.AsBoolean := False;
  end;
end;

I'm going to assume right away that this is bad code for pushing/pulling data from a table, any help would be appreciated. Also any debugging help would be amazing.

Upvotes: 2

Views: 536

Answers (1)

Mason Wheeler
Mason Wheeler

Reputation: 84550

If I were to guess, I'd say that it's this line:

tblSystemAutoUpdate.AsBoolean := False;

You're trying to edit the value of a dataset field without its dataset actually being open.

WRT debugging help, when you get the exception message and it asks you to Break or Continue, choose Break. The stack trace view (upper-left pane in the standard layout) will show you the function call stack of the current thread, and that should be able to lead you right to it. Also, make sure you build developer builds of your code with the "Use Debug DCUs" project option enabled. That will let you trace into RTL and VCL units in the debugger.

Upvotes: 2

Related Questions