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?
simple testcase that reproduces this behavior will be useful for understanding this issue.
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.
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?
TDAMemDataTable state is dsInsert mode. I want to restrict the record save and let user to complete the mandatory fields.
If I use TDAMemDataTable.Cancel, then it has an error.
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.
Here is call-stack.
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;
table.cancel; raise MyException.Create('');
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.
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
Any other solution to handle the cancel action?
try solution with exceptions and launch your client w/o debug mode for testing it.
you can try to update
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
Is it possible without error under debug mode?
Thanks for your advise. I use the other solution to solve the problem.