Page 1 of 1

Check for duplicates before post

Posted: Tue 05 Jan 2016 12:01
by Valgardur
Trying to detect posting of a duplicate key and presenting an application friendly error message I am running into problems..

I found two forum messages, one saying in OnPostErorr do something like ..
if (E is EMSError) and (EMSError(E).ErrorCode = ...) then ...
which is not of much help.

The other suggesting a bookmark and locate (which results in access violation error).

As far as I can tell, OnPostError only gets called after default message is displayed.. and with absolutely no information.

There is nothing on OnPostError in SDAC documentation.

Re: Check for duplicates before post

Posted: Fri 15 Jan 2016 08:49
by azyk
You can handle this error ( http://www.microsoft.com/products/ee/tr ... EvtID=2627 ) by the error code in a TMSQuery.OnPostError event handler using EMSError exceptions. The error code value is stored in the EMSError.MSSQLErrorCode property. For example:

Code: Select all

procedure TMainForm.MSQuery1PostError(DataSet: TDataSet; E: EDatabaseError;
  var Action: TDataAction);
begin
  // Violation of %ls constraint '%.*ls'. Cannot insert duplicate key in object '%.*ls'.   
  // (Microsoft SQL Server, Error: 2627)
  if (E is EMSError) and (EMSError(E).MSSQLErrorCode = 2627) then
  // DoSomething;
  ...
end;
See more details about OnPostError event handler in the Embarcadero documentation: http://docwiki.embarcadero.com/Librarie ... nPostError .

Re: Check for duplicates before post

Posted: Fri 15 Jan 2016 09:50
by Valgardur
1) As explained.. the error is displayed before OnPostError
2) Where do I find the error codes?
3) Where can I find documentation?

Re: Check for duplicates before post

Posted: Fri 15 Jan 2016 11:19
by azyk
Please try to compose a small sample reproducing the described behavior and send it to andreyz*devart*com . Include scripts for creating the test tables into the sample.

Re: Check for duplicates before post

Posted: Fri 15 Jan 2016 11:37
by Valgardur
I actually could not possibly wait the 10 days since I posted this.. so I already found a different approach, although I had to waste more time than acceptable. And to be honest, with the response time here it seems a waste of time to produce an example..

It would though, be helpful to know where to find those codes and where I can find documentation.

Re: Check for duplicates before post

Posted: Mon 18 Jan 2016 10:19
by azyk
You can find description of error codes generated by SQL Server in the MSDN documentation:
https://technet.microsoft.com/en-us/lib ... .105).aspx .