473,748 Members | 7,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

exception handling using SQLClient - is this the only exception that is caught???

Hi all,

I want to catch a duplicate key exception. I do not want to provide that
verbose message from the MSSQL server - I would put a user friendly message
out, like

"The item you have added already exists in the database. Please change
the description and try to save again."

instead of
System.Data.Sql Client.SqlExcep tion: Cannot insert duplicate key row in
object 'Tree' with unique index 'KeySiteParentT reeNodename'.
The statement has been terminated.
at System.Data.Sql Client.SqlComma nd.ExecuteReade r(CommandBehavi or
cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.Sql Client.SqlComma nd.ExecuteNonQu ery()
at ClaPub.DataAcce ss.ExecuteComma nd(String SQL, DataElements&
DataElements, BinaryDataEleme nts& BinaryDataEleme nts) in C:ClaPubClasses Data
Access ClassesDataAcce ss.vb:line 129

How do I do this? because the only error captured by .net is the general
System.Data.Sql Client.SqlExcep tion. I have tried using
System.Data.Dup licateNameExcep tion but that is not caught.

Try
SqlConnection.O pen()
m_ReturnValue = SqlCmdObj.Execu teNonQuery()
SqlConnection.C lose()
Catch e As System.Data.Sql Client.SqlExcep tion 'THIS IS CAUGHT
EVERY TIME
...
Catch e As System.Data.Dup licateNameExcep tion 'THIS IS NEVER
CAUGHT
...
Catch e As System.Data.NoN ullAllowedExcep tion 'THIS IS NEVER
CAUGHT
...
End Try

Nov 20 '05 #1
2 8906
"James Cooke" <ja************ ********@yahoo. com> schrieb
I want to catch a duplicate key exception. I do not want to provide
that verbose message from the MSSQL server - I would put a user
friendly message out, like

"The item you have added already exists in the database. Please
change
the description and try to save again."

instead of
System.Data.Sql Client.SqlExcep tion: Cannot insert duplicate key
row in
object 'Tree' with unique index 'KeySiteParentT reeNodename'.
The statement has been terminated.
[...]


The Errors property of the SqlException object probably contains the
error(s). Maybe checking the Number property of an SQLError object helps.

BTW, there is a specialized group for ADO.Net questions:
microsoft.publi c.dotnet.framew ork.adonet
--
Armin

Nov 20 '05 #2
exc.Message will give you a cleaner name. You could also write your own
exception class with whatever message you like, then throw a YourException
from SQLException.

Incidentally, the second two will not get caught when you have SQLClient
exception at the top because everything that could come from ExecuteNonQuery
(just about) will be caught by the SqlException first. NoNullAllowed would
only happend if you tried to insert a null into a DataTable, not Database
talbe, where the column was marked allownull = false.

Your in different libraries, but remember, always go from specific to broad
exception catches b/c the broad ones, if they are first ,will catch
everything more precise.

HTH,

Bill
"James Cooke" <ja************ ********@yahoo. com> wrote in message
news:ev******** ******@TK2MSFTN GP09.phx.gbl...
Hi all,

I want to catch a duplicate key exception. I do not want to provide that
verbose message from the MSSQL server - I would put a user friendly message out, like

"The item you have added already exists in the database. Please change
the description and try to save again."

instead of
System.Data.Sql Client.SqlExcep tion: Cannot insert duplicate key row in
object 'Tree' with unique index 'KeySiteParentT reeNodename'.
The statement has been terminated.
at System.Data.Sql Client.SqlComma nd.ExecuteReade r(CommandBehavi or
cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.Sql Client.SqlComma nd.ExecuteNonQu ery()
at ClaPub.DataAcce ss.ExecuteComma nd(String SQL, DataElements&
DataElements, BinaryDataEleme nts& BinaryDataEleme nts) in C:ClaPubClasses Data Access ClassesDataAcce ss.vb:line 129

How do I do this? because the only error captured by .net is the general
System.Data.Sql Client.SqlExcep tion. I have tried using
System.Data.Dup licateNameExcep tion but that is not caught.

Try
SqlConnection.O pen()
m_ReturnValue = SqlCmdObj.Execu teNonQuery()
SqlConnection.C lose()
Catch e As System.Data.Sql Client.SqlExcep tion 'THIS IS CAUGHT EVERY TIME
...
Catch e As System.Data.Dup licateNameExcep tion 'THIS IS NEVER
CAUGHT
...
Catch e As System.Data.NoN ullAllowedExcep tion 'THIS IS NEVER
CAUGHT
...
End Try

Nov 20 '05 #3

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

Similar topics

9
1326
by: chenchang | last post by:
Hi, experts. i write a simple program to test EH in G++: void throwfunction() { throw "test"; } class A { int i;
1
1733
by: linq936 | last post by:
Hi, I am using gcc3.2.3 on Red Hat Linux. I just find a strange thing of exception handling:if I compile the code in debug level, then the exception can be caught; if I compile it in optimized level , then the exception can not be caught. The compile option for debug is -c -DUNIX -fexceptions -g3 -DDEBUG -DLIN The compile option for optimize is
40
13522
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
6
1762
by: Vadivel Kumar | last post by:
I've a problem in handling a custom exception The following is my custom exception class: public class AppException : public Exception { public AppException (string message, Exception innerException) { } public override string Message
4
2040
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks like this: Try TryToDoIt() Catch e as Exception LogTheError(e)
11
5600
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
1
3031
by: metsys | last post by:
We have an ASP.NET 2.0 (C#) application that is divided into multiple layers. The multiple layers come from having a web project and 2 different class library projects in the same solution. I'm having difficulties figuring out the best way to handle (catch) exceptions in the different layers and then propagating those errors back up through the call stack to ultimately display something to the end-user. Note this is an intranet...
41
3074
by: Stuart Golodetz | last post by:
Hi all, Just wondering whether there's any reason why exception specifications are enforced at runtime, rather than at compile-time like in Java? (This was prompted by reading an article on GOTW, incidentally.) Is there something about C++ that makes it harder/impossible to check at compile-time? Cheers, Stu
2
2200
by: Carol | last post by:
Exception may be thrown in the code inside the try block. I want to handling the SqlException with State == 1 in a special way, and for all others I want to use a general way to handle. Which of the following options is better? ----------------------------------------------------- Option 1: try{...} catch (System.Data.SqlClient.SqlException ex) { if (ex.State == 1)
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
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...
1
9324
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
8243
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
6074
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
4606
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
3313
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
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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.