Presenting Error Messages

T

Todd Scott

Does anyone have a suggestion on how to present error
messages in a "user-friendly" way. I know how to "handle"
exceptions but what I want to do (at a minimum) is present
useful feedback to the user rather than a cryptic,
technical looking error message. For instance, if I
update a table (using SQLDataAdapter.UPDATE()) and it
returns a "Violation of UNIQUE KEY constraint..." message
I want to tell the user:

"User name entered already selected. Please select
another"

and not:

"Violation of UNIQUE KEY
constraint 'IX_Customer_Duplicate_User_Name'. Cannot
insert duplicate key in object 'Customer'.Source: .Net
SqlClient Data Provider"

I can not find a way to find out what constraint was
violated or what columns caused the error. I guess I
could parse the error message and then "hard-code" a
switch statement but that is not very "scalable"
or "object oriented".

Any suggestions?
 
R

Ravikanth[MVP]

Hi

Try in the following way:

Try
{//Code which generates Exception}
Catch(SQLException sqlexp)
{
sqlexp.Number //This number corresponds to an entry in
the master.dbo.sysmessages table.
//depeding on the Number give friendly error message

}


Ravikanth
 
T

Todd

Thank you but that would not allow me to specify the column name(s)
that caused the violation. In fact, it would just present the
"skeleton" for the message that already appears.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top