Data Connection Failed – Error Handling

When a connection to a data source fails (because the path is wrong or the database is unavailble) I would like to present the user with a friendly error message rather than the “unhandled error” message from Windows.  I am looking for help with some C# code to efficiently handle a connection error and maybe provide a message that identifies the type of error.  Any help is greatly appreciated!
Who is Participating?
Si_HibbardConnect With a Mentor Commented:
use try and catch around the code you are executing, eg:

// your code
catch(exception e)
// check value of e and show some status label depending on it, or just show a value from e
// statuslable.text = "your text"
// statuslabel.visible = true;
I would use a try/catch but also use the classes derived from DbException which can then give you more details. For example SqlException has a property Number. The details on these error messages are in master.dbo.sys.messages
dev775Author Commented:
Would it be possible to walk through an example?  For instance in my application the user must enter the path to the data files and clicks a button to connect to the database.  If the wrong path is entered and the button event cannot connect to the db, how would I handle this type of event?
dev775Author Commented:
Never received a response to my follow-up request.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.