retreive data from access - try and catch?

I am building pages similiar to the concept behind this page listed here. http://support.microsoft.com/default.aspx?scid=kb;EN-US;q308100&GSSNB=1
I was wondering if try and catch statements should be used in the pages, I always though it is standard practice to include this. Why isnt it used in this example?
What is the strandard protocol and process for this?

Or does it not merrit adding this since  its just a query of data?

LVL 1
chicabowAsked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
What more do you need to know ?

Ordinarily you would add a try...catch around any database access code due to its nature and the myriad of things that can go wrong:

    SqlConnection cn = new SqlConnection("Connection string");

    try
    {
          cn.Open();
             
          // Other database access code
    }
    catch (SqlException ex)
    {
         // Report and/or log the exception
    }
    finally
    {
         if (cn.ConnectionState != ConnectionState.Closed)
             cn.Close();
    }


As I mentioned earlier, most of the Microsoft samples don't include try...catch code simply for brevity and just stick to demonstrating the specific point in question.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Because they're being lazy :o)

You'll find a lot of samples don't bother putting the try...catch in there so that you concentrate on the specific concept they are trying to demonstrate.
0
 
chicabowAuthor Commented:
Looking for some more concrete answer if possible anyone?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Slight correction:

         if (cn.State != ConnectionState.Closed)
             cn.Close();
0
 
chicabowAuthor Commented:
Thanks for the reminder, these are helpful for people who foget to close them out.

Thanks Carl_Tawn
0
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.