We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

More friendlier OleDb error message.

fkmfkm
fkmfkm asked
on
Medium Priority
239 Views
Last Modified: 2011-10-03
Instead of the error message

"The statement has been terminated. Violation of PRIMARY KEY....."

Is there anyway to trap different types of error (...primary key, length too long, unique constraint) and provide a nicer error message to the user.

How do you guys do it ? can share ?
Comment
Watch Question

Most of the time, these kind of things happen when you want to perform some kind of sequence of actions. E.g. a user tried to store a product he just designed.
Does he want to know a primary key was violated?
No, he wants to know just if his product was stored or not.
Your application might write the real message to a log or something like that. So in case your angry customer calls you, you have a way to sort it out :D

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Try

Catch ex As OleDbException

  Throw New Exception("This is a better exception message")

End Try

Bob

Author

Commented:
Thanks Pocky and LearnedOne,

I guess the answer is the combination of both your suggestions.

TheLearnedOne,

Is there anyway to catch more specific error eg. Primary Key violation, Secondary ..etc ?

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
While this is not a comprehensive list, these are the data exception that I have found so far:

System.Data      ConstraintException
System.Data      DataException
System.Data      DBConcurrencyException
System.Data      DeletedRowInaccessibleException
System.Data      DuplicateNameException
System.Data      InRowChangingEventException
System.Data      InvalidConstraintException
System.Data      InvalidExpressionException
System.Data      MissingPrimaryKeyException
System.Data      NoNullAlllowedException
System.Data      ReadOnlyException
System.Data      RowNotInTableException
System.Data      StringTypingException
System.Data      TypedDataSetGeneratorException
System.Data      VersionNotFoundException
System.Data.OleDb      OleDbException

Bob

Author

Commented:
Thanks TheLearnedOne,

A step closer now..

Ok with SQL I always have this problem. When my data exceeds the lenght of the field..I will get some error message .. The problem is I do not know which field is this that is exceeded.

So to make my problem more general..is there away (easy though) to highlight to there user some thing more specific like.."Data Address is too long" etc...Something so that the user knows how to rectify the problem himself.



Thank you.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
If you are wanting that information, you are going to have write some code to check for string columns, string lengths, and compare them against the defined size for the corresponding column in the table.

Bob

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.