Recent

Author Topic: Is it timing of a dataset?  (Read 1875 times)

CraigC

  • Jr. Member
  • **
  • Posts: 78
Is it timing of a dataset?
« on: April 26, 2026, 02:41:44 pm »
I have a table that I have an edit form setup for. When I go back to edit a couple fields and press save and exit, I get dataset not in dsEdit or dsInsert.  If I go and try to do a refresh, I can't because it has pending updates.   Anything I can do to sync the data components?

Lazarus 4.6, Linux  SQLdb components.

Thanks,

Craig

CraigC

  • Jr. Member
  • **
  • Posts: 78
Re: Is it timing of a dataset?
« Reply #1 on: April 26, 2026, 02:43:21 pm »

As soon and I press send, I remembered to mention the database.  SQLite 3.

JanRoza

  • Hero Member
  • *****
  • Posts: 747
    • http://www.silentwings.nl
Re: Is it timing of a dataset?
« Reply #2 on: April 26, 2026, 04:24:39 pm »
Show some code and not just an error message.
I think the second message says it all, did you do an AplyUdates after posting?
OS: Windows 11 / Linux Mint 22.3
       Lazarus 4.6 RC FPC 3.2.2
       CodeTyphon 8.90 FPC 3.3.1

CraigC

  • Jr. Member
  • **
  • Posts: 78
Re: Is it timing of a dataset?
« Reply #3 on: April 26, 2026, 04:35:32 pm »

I don't call qry.post.  Never have with SQLite and Lazarus.  Trying to eliminate the error "Not in Edit or Insert mode"

Here is my procedure for Saving or Canceling updates. 

procedure SaveData(qry: TSQLQuery; bSave: Boolean);
var
  s: string;
begin
  s := qry.Name;
  try
    if not dmCommonData.transLedger.Active then
      dmCommonData.transLedger.StartTransaction;
    if bSave then
    begin
      if DataState(qry) then
      begin
        qry.ApplyUpdates;
        dmCommonData.transLedger.Commit;
      end;
    end
    else
    begin
      qry.CancelUpdates;
      dmCommonData.transLedger.Commit;
    end;
  except
  on E: EDatabaseError do
     begin
       dmCommonData.transLedger.Rollback;
       MessageDlg('Error', 'DataBase error: '+ E.Message, mtError, [mbOK], 0);
     end;
  end;
end;

cdbc

  • Hero Member
  • *****
  • Posts: 2787
    • http://www.cdbc.dk
Re: Is it timing of a dataset?
« Reply #4 on: April 26, 2026, 04:36:04 pm »
Hi
Are you using Db-Aware-components?
If so -- alas, I can't help you.
I'm running SQLite3 in more apps without db-aware and it all goes like stink... 8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

CraigC

  • Jr. Member
  • **
  • Posts: 78
Re: Is it timing of a dataset?
« Reply #5 on: April 26, 2026, 05:08:12 pm »

Yes, these are TDBEdits.  I'm finding that in one instance, I use TEdits and found I had issues with TDBEdits and TDBgrid.  I'm getting close to finding something else to update my old code from Delphi 7.  DBedits that were for string fields, would go blank and integers edits would be just fine. 

Frustrated beyond belief.

CraigC

  • Jr. Member
  • **
  • Posts: 78
Re: Is it timing of a dataset?
« Reply #6 on: April 26, 2026, 05:10:07 pm »

I'm also finding that doing an insert goes great, but updates are really where Lazarus is buggy.

LemonParty

  • Sr. Member
  • ****
  • Posts: 470
Re: Is it timing of a dataset?
« Reply #7 on: April 26, 2026, 05:16:41 pm »
Code: Pascal  [Select][+][-]
  1.     begin
  2.       qry.CancelUpdates;
  3.       dmCommonData.transLedger.Commit;
  4.     end;
I don't think you have to do Commit after you CancelUpdates.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

cdbc

  • Hero Member
  • *****
  • Posts: 2787
    • http://www.cdbc.dk
Re: Is it timing of a dataset?
« Reply #8 on: April 26, 2026, 05:21:07 pm »
Hi
You could take a look at the attached demo, for inspiration...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

rvk

  • Hero Member
  • *****
  • Posts: 7018
Re: Is it timing of a dataset?
« Reply #9 on: April 27, 2026, 12:18:07 pm »
Code: Pascal  [Select][+][-]
  1.     begin
  2.       qry.CancelUpdates;
  3.       dmCommonData.transLedger.Commit;
  4.     end;
I don't think you have to do Commit after you CancelUpdates.
I think you can... but I would do a rollback there. But even so.... before Commit or Rollback I always check the state first.

Code: Pascal  [Select][+][-]
  1.     begin
  2.       qry.CancelUpdates;
  3.       if dmCommonData.transLedger.Active then dmCommonData.transLedger.Rollback;
  4.     end;

And if you need to edit it directly again I would use CommitRetaining or RollbackRetaining.

BTW. Also check the SQL component for the options (AutoApplyUpdates, AutoCommit, ImplicitTransaction, etc).
« Last Edit: April 27, 2026, 12:20:00 pm by rvk »

LemonParty

  • Sr. Member
  • ****
  • Posts: 470
Re: Is it timing of a dataset?
« Reply #10 on: April 27, 2026, 01:53:30 pm »
Commiting changes whan you don't want them look like mistake in programs logic. Let's say we have 2 tables and we edited the first one then we edited the second table and than we want to discard changes from both tables. Rollback in this situation is reasonable, but if we do Commit then changes from the first table will be saved and from the second one will be lost. This logic is not intuitive for user, so Commit should be avoided.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

CraigC

  • Jr. Member
  • **
  • Posts: 78
Re: Is it timing of a dataset?
« Reply #11 on: April 27, 2026, 02:11:13 pm »
I modified the code for Rollback.

Are you saying I should not use Commit for applying updates?  Most of my issues are getting updates to save and not so much for inserting.

rvk

  • Hero Member
  • *****
  • Posts: 7018
Re: Is it timing of a dataset?
« Reply #12 on: April 27, 2026, 02:22:24 pm »
Are you saying I should not use Commit for applying updates?  Most of my issues are getting updates to save and not so much for inserting.
No, you schouldn't use Commit after a cancel. With cancel you want to rollback any changes made in the transaction.

Besides that, we don't see the complete (flow of) code. When is this called? What does DataState() do? What does the local variable s do? etc.

You also have "if not dmCommonData.transLedger.Active then dmCommonData.transLedger.StartTransaction;" in there... but... when calling SaveData I assume the dataset is already in edit mode (it switches to edit mode as soon as you begin editing). So starting a transaction there is far too late (I suspect the StartTransaction is never called there because the transaction should already have been started at first edit of the data-aware edit-box).

CraigC

  • Jr. Member
  • **
  • Posts: 78
Re: Is it timing of a dataset?
« Reply #13 on: April 27, 2026, 02:33:38 pm »

function DataState(ds: TDataSet): Boolean;
begin
  if ds.State in [dsEdit, dsInsert] then
    Result := True
  else
    Result := False;
end;

Yeah, dataset should be in dsEdit or dsInsert.  I don't call post anywhere.   Transaction should be active but I get errors that the dataset is not in dsEdit or dsInsert.  The indicator on the dbgrid, tells a different story.  It shows edit mode.

rvk

  • Hero Member
  • *****
  • Posts: 7018
Re: Is it timing of a dataset?
« Reply #14 on: April 27, 2026, 02:47:23 pm »
Transaction should be active but I get errors that the dataset is not in dsEdit or dsInsert.  The indicator on the dbgrid, tells a different story.  It shows edit mode.
On what line do you get the error?

BTW. not sure about SQLite, but it could be that Commit and Rollback also closes the dataset.
(unless you have sqoKeepOpenOnCommit in the TSQLQuery.Options)

You can trace/debug through the program and see where the qry.Sate flips to dsInactive or dsBrowse.

 

TinyPortal © 2005-2018