views:

210

answers:

3

Hi

I just uploaded my first ASP.NET (as part of my learning of vb.net) and got into awful mess with the connection pooling (funny things happen when there are more than 1 user of your web site) all sorted now by better use of the try catch statements (well the idea was to learn) BUT I was wondering if this is the best / final method, now if the try fails, then a LOT of the detail on the page isn't placed/updated, so if you are doing some database work and the try fails, do you reload the page ... redirect to self and hope it work the next time ... or just inform the user there was a error and they should try again ?

Thanks

A: 

I would never automatically redirect and hope that it will work the next time (you might get in a infinite loop).

Inform the user and optionally a link to try it again.

You even might want to analyze your exception to see if another try will help. Some exceptions are really bugs, and another try will not help.

Michiel Overeem
A: 

If an unexpected error occurs, redirect the user to an error page as it probably will happen the next time around too.

Have you looked into "Using" statements for DB connections and readers?

Robert Wagner
A: 

You should defintely use 'using' statements for all objects that implement IDisposable (such as connections and readers). A 'using' statement gets translated into a try-finally block under the covers, and ensures that Dispose() is called even if an error occurs.

Here is a code snippet example:

using (SqlConnection conn = new SqlConnection(this.connectionString))
{
    using (SqlCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText = "LoadFromRepository";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ID", fileID);

        conn.Open();
        using (SqlDataReader rdr =
                   cmd.ExecuteReader(CommandBehavior.CloseConnection))
        {
            while (rdr.Read())
            {
                // do something with read data
            }
        }
    }
}
Mitch Wheat