Errorcode from SQL Server

Discussion of open issues, suggestions and bugs regarding usage of dbExpress drivers for SQL Server in Delphi and C++Builder
Post Reply
Jelly
Posts: 18
Joined: Sat 09 Jul 2005 13:49
Contact:

Errorcode from SQL Server

Post by Jelly » Thu 05 Oct 2006 14:29

When a SQL command fails due to an error (key violoation, wrong syntac etc.), I get an exception under Delphi. The following code reads the errormessage from the server.

Code: Select all

try
   qry.post ;
except
   on e: exception do
       showmessage(e.message) ;
end ;
But I could not find a way, to get the SQL Server Errorcode.

When I try to use the following code, the except section never seems to be executed, allthogh EDBClient is derived from exception.

Code: Select all

try
   qry.post ;
except
   on e: EDBClient do
       showmessage(inttostr(e.errorcode)) ;
end ;
What can I do to get the errorcode ?

Jackson
Posts: 512
Joined: Thu 26 Jan 2006 10:06

Post by Jackson » Fri 06 Oct 2006 12:54

Try to use EMSError class instead of EDBClient. Do not forget to include OLEDBAccess.pas unit.

Code: Select all

uses
  ...OLEDBAccess;
...
  try
    // your code
  except
    on E: EMSError do begin
      // E.ErrorCode
    end;
  end;

Jelly
Posts: 18
Joined: Sat 09 Jul 2005 13:49
Contact:

Post by Jelly » Sat 07 Oct 2006 08:37

Thanks for this. I will try this only on Monday at work and give you feedback.

Jelly
Posts: 18
Joined: Sat 09 Jul 2005 13:49
Contact:

Post by Jelly » Mon 09 Oct 2006 06:37

EvgeniyM wrote:

Code: Select all

uses
  ...OLEDBAccess;
...
This OLEDBAccess.pas file does not exist on my system. Where can I find it?

Jackson
Posts: 512
Joined: Thu 26 Jan 2006 10:06

Post by Jackson » Mon 09 Oct 2006 11:56

Sorry for measleading. This solution is correct only for SDAC.
Please specify type of DataSet component which you use in your example.

Jelly
Posts: 18
Joined: Sat 09 Jul 2005 13:49
Contact:

Post by Jelly » Mon 09 Oct 2006 13:28

I'm using the components from the dbExpress pallet, namely SQLQuery, SimpleDataset and SQLStoredProc from unit SqlExpr.

Jackson
Posts: 512
Joined: Thu 26 Jan 2006 10:06

Post by Jackson » Tue 10 Oct 2006 05:52

Use TCustomClientDataSet.OnReconcileError event handler to handle any update error.

Jelly
Posts: 18
Joined: Sat 09 Jul 2005 13:49
Contact:

Post by Jelly » Wed 11 Oct 2006 06:51

EvgeniyM wrote:Use TCustomClientDataSet.OnReconcileError event handler to handle any update error.
TSQLQuery doesn't have this event.

I think I didn't explain clearly enough what I was lookign for. OnReconcile Error handles the errors only that are are described in the held like this:
Delphi-Help wrote:Write an OnReconcileError event handler to respond to error conditions that arise when the Reconcile method cycles through records that could not be applied to the database. This event handles only those update errors that were not handled in an OnUpdateError event handler.
What I am looking for is the real SQL server Msg Number when an error occurs. Trying to use this SQL statement e.g., an violoation to a unique key constraint might occur, and the SQL Query Analyzer gives me the following:

Code: Select all

insert into personen (Name) values ('Test')

Code: Select all

Server: Msg 2627, Level 14, State 1, Line 1
Violation of UNIQUE KEY constraint 'IX_Personen'. Cannot insert duplicate key in object 'dbo.personen'.
The statement has been terminated.
I have to catch this server error N° 2627.

How can I do this?

Jelly
Posts: 18
Joined: Sat 09 Jul 2005 13:49
Contact:

Post by Jelly » Thu 12 Oct 2006 15:41

Sorry for pushing. Does anybody have an idea how to catch the errorcode?

Jackson
Posts: 512
Joined: Thu 26 Jan 2006 10:06

Post by Jackson » Fri 13 Oct 2006 10:02

You can't get server error code when you use TSQLQuery or TSQLStoredProc.
This is restriction of dbExpress technology (see ISQLCursor interface).
If you need error code you can parse error message to get it.

Jelly
Posts: 18
Joined: Sat 09 Jul 2005 13:49
Contact:

Post by Jelly » Fri 13 Oct 2006 11:51

EvgeniyM wrote:You can't get server error code when you use TSQLQuery or TSQLStoredProc.
This is restriction of dbExpress technology
:shock: I can't understand this, but it just seems to be fact I have to accept.
EvgeniyM wrote:If you need error code you can parse error message to get it.
There is no number inside the error message. The message just contains the text, the same as in Query Analyzer, but no message Nr.

It looks like I just have to forget about this ides to catch the error Nr :twisted:

Post Reply