473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.SqlClient.SqlException: Cannot insert duplicate key row in
object 'Tree' with unique index 'KeySiteParentTreeNodename'.
The statement has been terminated.
at System.Data.SqlClient.SqlCommand.ExecuteReader(Com mandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at ClaPub.DataAccess.ExecuteCommand(String SQL, DataElements&
DataElements, BinaryDataElements& BinaryDataElements) in C:ClaPubClassesData
Access ClassesDataAccess.vb:line 129

How do I do this? because the only error captured by .net is the general
System.Data.SqlClient.SqlException. I have tried using
System.Data.DuplicateNameException but that is not caught.

Try
SqlConnection.Open()
m_ReturnValue = SqlCmdObj.ExecuteNonQuery()
SqlConnection.Close()
Catch e As System.Data.SqlClient.SqlException 'THIS IS CAUGHT
EVERY TIME
...
Catch e As System.Data.DuplicateNameException 'THIS IS NEVER
CAUGHT
...
Catch e As System.Data.NoNullAllowedException 'THIS IS NEVER
CAUGHT
...
End Try

Nov 20 '05 #1
2 8881
"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.SqlClient.SqlException: Cannot insert duplicate key
row in
object 'Tree' with unique index 'KeySiteParentTreeNodename'.
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.public.dotnet.framework.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**************@TK2MSFTNGP09.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.SqlClient.SqlException: Cannot insert duplicate key row in
object 'Tree' with unique index 'KeySiteParentTreeNodename'.
The statement has been terminated.
at System.Data.SqlClient.SqlCommand.ExecuteReader(Com mandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at ClaPub.DataAccess.ExecuteCommand(String SQL, DataElements&
DataElements, BinaryDataElements& BinaryDataElements) in C:ClaPubClassesData Access ClassesDataAccess.vb:line 129

How do I do this? because the only error captured by .net is the general
System.Data.SqlClient.SqlException. I have tried using
System.Data.DuplicateNameException but that is not caught.

Try
SqlConnection.Open()
m_ReturnValue = SqlCmdObj.ExecuteNonQuery()
SqlConnection.Close()
Catch e As System.Data.SqlClient.SqlException 'THIS IS CAUGHT EVERY TIME
...
Catch e As System.Data.DuplicateNameException 'THIS IS NEVER
CAUGHT
...
Catch e As System.Data.NoNullAllowedException '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
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
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...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
6
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...
4
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...
11
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
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...
41
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.