Solved

Exception - hot to get SQLServer Err. number

Posted on 2000-05-18
7
388 Views
Last Modified: 2010-04-04
I'm using D5 and ADO components to access SQL Server. Is there a way to retrieve SQL server error code, when exception is raised? For example: I use ADOQuery to execute stored procedure, and the procedure is missing, how can I catch this error?
0
Comment
Question by:pvs001d
  • 2
  • 2
  • 2
  • +1
7 Comments
 

Author Comment

by:pvs001d
ID: 2824423
Adjusted points from 50 to 100
0
 
LVL 2

Expert Comment

by:IPCH
ID: 2824558
Can you send part of you code?
0
 
LVL 15

Accepted Solution

by:
simonet earned 100 total points
ID: 2925829
You should use the TADOStoredProc component.

After you call the EXecProc method, the parameter @RESPOST (or something like that... it's always the 1st parameter in the TADOStoredProc parameters list) will be filled with the result code of the execution.

Just read its value and you know what happened. The Online Books for SQL Server 7 lists all the error codes that can be returned.

Alex
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 15

Expert Comment

by:simonet
ID: 2930573
Correction:

the parameter is called @RETURN_VALUE and every stored procedure automatically defines this parameter. Remember that you must set its direction as output in the TADOStoredProc's Parameter's editor.

Alex
0
 

Author Comment

by:pvs001d
ID: 3431312
Answer accepted
0
 

Expert Comment

by:radu_antohi
ID: 9204001
The paramater is called @RETURN_VALUE, but the parameter direction is not 'pdOutput', but 'pdReturnValue'.

Delphi Example:

var
  Ret: Integer;
begin
  ADOConnection1.Connected := True;

  ADOStoredProc1.Parameters.Clear;
  ADOStoredProc1.Parameters.CreateParameter('@RETURN_VALUE', ftInteger, pdReturnValue, 0, NULL);

  ADOStoredProc1.ExecProc;

  Ret := ADOStoredProc1.Parameters.ParamByName('@RETURN_VALUE').Value;

  ShowMessage(IntToStr(Ret));
end;
0
 

Expert Comment

by:radu_antohi
ID: 9204093
Another way to get the error id is from the exception raised with RAISERROR by the SQL Server.

try
 SP.ExecProc;
except
 on E: EOleException do
 begin
  if (ADOConnection.Errors.Count > 0) then
  begin
   NativeError := ADOConnection.Errors[0].NativeError;
   Description := ADOConnection.Errors[0].Description;
  end
 end;
end;

The best way for error handling is to use both methods (exceptions raised with RAISERROR and RETURN_VALUE), because there are exceptions that are not catched by tke OLEDb provider or by Delphi. I don't know where is the problem.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question