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!
dev775Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Si_HibbardCommented:
use try and catch around the code you are executing, eg:

try{
// 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;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stagepCommented:
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
0
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?
0
dev775Author Commented:
Never received a response to my follow-up request.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.