How to cancel table post

Hi,
I added TDAMemDataTable.Cancel in “OnBeforePost” if condition is true. But I got an error message
“Dataset not in edit or insert mode”. Anything is wrong?

Hi,

what condition?

simple testcase that reproduces this behavior will be useful for understanding this issue.

Hi,
The condition is checking any record exist in table. If exist, then cancel the record post and prompt the warning message. User needs to revise record until record doesn’t exist in table.

Hi,

I’d suggest to check call-stack and see what code causes that error.
error can be in std TDataset.Post method if you just cancel record w/o raising exception.

I think the issue in question is that on OnBeforePost, the row should be in Edit/Insert mode, no?

Hi,
TDAMemDataTable state is dsInsert mode. I want to restrict the record save and let user to complete the mandatory fields.

image

If I use TDAMemDataTable.Cancel, then it has an error.
image

Hi,

You haven’t show call-stack.

try to raise exception after doing TDAMemDataTable.Cancel, it will allow to cancel execution of current logic.


by other hand, you can use ruoOnPost logic. If error is occupied during inserting/updating record, it will suggest to continue editing and fix error. of course, you should have correspondent foreign keys or validate record on trigger level.

Hi,

Here is call-stack.
image

Hi,

as expected.
you can’t just call table.Cancel;. Raise exception for terminate execution of other code.

try
  table.Post;
except
   on e: MyException do ;
   on e: Exception do raise;
end;

your handler:

  table.cancel;
  raise MyException.Create('');

Hi,
I have validation and prompt the warning message before post the record. Raise exception is a duplicate action for user.

I use cancel in Tbutton without error.

Hi,

this error will be visible only in debug mode and invisible for end-user and required for breaking default DataAbstract logic in case you have cancelled changes in OnBeforePost event

Hi,
Any other solution to handle the cancel action?

Hi,

try solution with exceptions and launch your client w/o debug mode for testing it.


you can try to update uDADataTable.pas as

procedure TDADataTable.InternalBeforePost(Sender: TDataset);
...
  if State in dsEditModes then //<<<<<<<<<added
  TempSetRowRecIDValue(Self.Dataset); // set fRecIDField.AsInteger & CurrRecId

it should remove original error , but side effect can be present

Hi,
Is it possible without error under debug mode?

Hi,

you can ignore MyException in Delphi Debugger:

Hi,
Thanks for your advise. I use the other solution to solve the problem.