473,725 Members | 2,276 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Database Error Handling - please help

Hi All,

I have a couple of stored procedures written in SQL 2000. Given below one sp.
*************** *************** *************** ********
ALTER PROCEDURE [dbo].[usp_PC_AcTypeMI nsert]
(
@AcTypeName varchar(50),
@GLCode varchar(50),
@ErrorCode int output

)
AS
SET NOCOUNT OFF;
BEGIN TRAN

INSERT INTO [PC_AcTypeM]
([AcTypeName],
[GLCode])
VALUES (@AcTypeName, @GLCode);
SELECT @ErrorCode = @@ERROR
IF @ErrorCode <0 GOTO ERR_HANDLER
SELECT @ErrorCode = 0
COMMIT TRAN
ERR_HANDLER:
ROLLBACK TRAN
*************** *************** *************** *********
Please note that I have an output parameter for the SP, @ErrorCode to return
the ErrorCode if any error occurs.

I have a unique key constraint on the column named "AcTypeName ". Now, when
execute this SP from my C# code, using Enterprise library, trying to insert a
new AccountTypeName which is already there in the table, it gives an error.
But the problem is that the execution control goes to the exception handler,
where I retrieve the @ErrorCode value. Is it right ? Is it possible to
retrieve this error in the main execution block without going to the
Exception Handling block ?

Attached a piece of C# code for executing this SP.

*************** *************** *************** ***********
public void InsertAccountTy pe(PC_AcTypeM Actype, out int
intAccTypeError Code)
{
SqlDatabase db = new SqlDatabase(str Con);
string sqlCommand = "usp_PC_AcTypeM Insert";
DbCommand dbCommand = db.GetStoredPro cCommand(sqlCom mand);
try
{
db.AddInParamet er(dbCommand, "@AcTypeNam e", DbType.String,
Actype.AcTypeNa me);
db.AddInParamet er(dbCommand, "@GLCode", DbType.String, Actype.GLCode);
db.AddOutParame ter(dbCommand, "@ErrorCode ", DbType.Int32, 0);
db.ExecuteNonQu ery(dbCommand);
intAccTypeError Code = int.Parse(db.Ge tParameterValue (dbCommand,
"@ErrorCode").T oString());
}
catch (Exception ex)
{
intAccTypeError Code = int.Parse(db.Ge tParameterValue (dbCommand,
"@ErrorCode").T oString());
}
}
*************** *************** *************** **************

Can anyone throw some idea on this and help me to get to the right way to
achieve this ?

Thanks & Regards
Suhas

Aug 28 '06 #1
2 4152
It is probably still getting the error from the server and responding
to that. Reading @@ERROR doesn't stop the error coming back over the
wire, and all over a certain severity are treated as exceptions.

Simply stop your SQL from erroring! (check things first, perhaps using
UPDLOCK, etc).

Marc

Aug 28 '06 #2
=?Utf-8?B?U3VoYXMgVmV uZ2lsYXQ=?=
<Su***********@ discussions.mic rosoft.comwrote in
news:F6******** *************** ***********@mic rosoft.com:
Hi All,

I have a couple of stored procedures written in SQL 2000. Given below
one sp. *************** *************** *************** ********
ALTER PROCEDURE [dbo].[usp_PC_AcTypeMI nsert]
(
@AcTypeName varchar(50),
@GLCode varchar(50),
@ErrorCode int output

)
AS
SET NOCOUNT OFF;
BEGIN TRAN

INSERT INTO [PC_AcTypeM]
([AcTypeName],
[GLCode])
VALUES (@AcTypeName, @GLCode);
SELECT @ErrorCode = @@ERROR
IF @ErrorCode <0 GOTO ERR_HANDLER
SELECT @ErrorCode = 0
COMMIT TRAN
ERR_HANDLER:
ROLLBACK TRAN
*************** *************** *************** *********
Please note that I have an output parameter for the SP, @ErrorCode to
return the ErrorCode if any error occurs.

I have a unique key constraint on the column named "AcTypeName ". Now,
when execute this SP from my C# code, using Enterprise library, trying
to insert a new AccountTypeName which is already there in the table,
it gives an error. But the problem is that the execution control goes
to the exception handler, where I retrieve the @ErrorCode value. Is it
right ? Is it possible to retrieve this error in the main execution
block without going to the Exception Handling block ?

Attached a piece of C# code for executing this SP.

*************** *************** *************** ***********
public void InsertAccountTy pe(PC_AcTypeM Actype, out int
intAccTypeError Code)
{
SqlDatabase db = new SqlDatabase(str Con);
string sqlCommand = "usp_PC_AcTypeM Insert";
DbCommand dbCommand = db.GetStoredPro cCommand(sqlCom mand);
try
{
db.AddInParamet er(dbCommand, "@AcTypeNam e", DbType.String,
Actype.AcTypeNa me);
db.AddInParamet er(dbCommand, "@GLCode", DbType.String,
Actype.GLCode);
db.AddOutParame ter(dbCommand, "@ErrorCode ", DbType.Int32,
0); db.ExecuteNonQu ery(dbCommand);
intAccTypeError Code =
int.Parse(db.Ge tParameterValue (dbCommand,
"@ErrorCode").T oString());
}
catch (Exception ex)
{
intAccTypeError Code =
int.Parse(db.Ge tParameterValue (dbCommand,
"@ErrorCode").T oString());
}
}
*************** *************** *************** **************

Can anyone throw some idea on this and help me to get to the right way
to achieve this ?

Thanks & Regards
Suhas
You must handle the exception, Secondly, you can get the error number by
catching the SqlException, using catch(SqlExcept ion sqlEx)
{...sqlEx.Numbe r...} and assign it to the variable.... Something like
this. Of course I haven't used the output parameter.

*************** *************** *************** *************** ****

intAccTypeError Code = 0;

try
{
cmd.Parameters. Add("@AcTypeNam e", SqlDbType.VarCh ar, 20, "AcTypeName ");
cmd.Parameters. Add("@GLCode", SqlDbType.VarCh ar, 20, "GLCode");

cmd.Parameters["@AcTypeNam e"].Value = Actype;
cmd.Parameters["@GLCode"].Value = GLCode;

cnn.Open();

cmd.ExecuteNonQ uery();
}
catch(SqlExcept ion sqlEx)
{
intAccTypeError Code = sqlEx.Number;
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
finally
{
cnn.Close();
}

// Do something with the error code outside the catch block
this.txtErrorCo de.Text = intAccTypeError Code.ToString() ;

*************** *************** *************** *************** ****
--
Regards
JTC ^..^
Aug 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4772
by: Bill S. | last post by:
Hi, I a stored procedure that inserts a record into a table as below. The insert works OK, but if the insert violates a unique indewx constraint on one of the columns, the proc terminates immediately, and does NOT execute the 'if @@ERROR <> 0' statement.
6
7341
by: Marvin Libson | last post by:
Hi All: I am running DB2 UDB V7.2 with FP11. Platform is Windows 2000. I have created a java UDF and trigger. When I update my database I get the following error: SQL1224N A database agent could not be started to service a request, or was terminated as a result of a database system shutdown or a force command.
3
3025
by: Sam | last post by:
I have developed a small database which is setup with the front end and backend files. There are 12 linked tables withe the datafile located on our server. Now the boss wants to be able to take a laptop offsite and still have access, (even if read only) to the main data file (to be able to add records in 1 table would also be useful). I would like to be able to get the user to press a command button which
9
2438
by: Therese A. Sorna | last post by:
Hello all... I am using Access 2002. I have database set to add users names to the user level security when they are added to a particular table in a database, using the Catalog and ActiveConnection commands. I am getting errors when I try to add a name that is already in the database. I was wondering if anyone knew of a way to have it check for the user's name before going to add it? One method would be to delete everytime before...
7
2236
by: News | last post by:
Hello, I have to build a program with the future in mind and I need a bit of guidance from a guru or two. My program will start as a multi-user Windows Application built with VB.Net and using an Access 2002 database backend. The future will require that 1. The database be switched with minimal effort to SQL Server and 2. A Web Application be added to allow web access to reports generated from the database. At this time, there is no...
9
2227
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that I have been doing it right. I think I overdo it. Please have a look: -- using System; using System.IO;
3
2123
by: Marvinq | last post by:
I am working on an Access 2002/Oracle vba automated overnight process. The problem is there is one user with rights to a certian set of tables and another user with a certain set of tables. I can't run the overnight process with just one login. Currently, I am having problems with relogging into the database with a different login. Even when I enter a different userid and password into a connection string, the system seems to still use...
0
2024
by: poojamangal | last post by:
I want to upload images or pdf files. but i m unable to do so. i got error. please help me to sort it out.. my code is: <% 'on error resume next Class FreeASPUpload Public UploadedFiles Public FormElements Private VarArrayBinRequest
1
1651
BezerkRogue
by: BezerkRogue | last post by:
I have created a script that automates the launching of an application based upon a value in the registry. For some reason, it is not connecting to the database to read what application it's supposed to launch. I have combed over this script and can't figure out what the issue is. Please help. ' VBScript File for synchronizing error proofing applications and launching the correct EP application on station start up. 'Error handling...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.