Check for duplicates before post

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Valgardur
Posts: 164
Joined: Tue 24 Nov 2009 19:33

Check for duplicates before post

Post by Valgardur » Tue 05 Jan 2016 12:01

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.

azyk
Devart Team
Posts: 1057
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: Check for duplicates before post

Post by azyk » Fri 15 Jan 2016 08:49

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 .

Valgardur
Posts: 164
Joined: Tue 24 Nov 2009 19:33

Re: Check for duplicates before post

Post by Valgardur » Fri 15 Jan 2016 09:50

1) As explained.. the error is displayed before OnPostError
2) Where do I find the error codes?
3) Where can I find documentation?

azyk
Devart Team
Posts: 1057
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: Check for duplicates before post

Post by azyk » Fri 15 Jan 2016 11:19

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.

Valgardur
Posts: 164
Joined: Tue 24 Nov 2009 19:33

Re: Check for duplicates before post

Post by Valgardur » Fri 15 Jan 2016 11:37

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.

azyk
Devart Team
Posts: 1057
Joined: Fri 11 Apr 2014 11:47
Location: Alpha Centauri A

Re: Check for duplicates before post

Post by azyk » Mon 18 Jan 2016 10:19

You can find description of error codes generated by SQL Server in the MSDN documentation:
https://technet.microsoft.com/en-us/lib ... .105).aspx .

Post Reply