Solved

Try Catch Timeout

Posted on 2013-10-23
11
680 Views
Last Modified: 2013-10-24
I am getting a timeout error somewhere in a code block. The block has about 40 lines of code.

What is the proper way to catch the error?

try
{
//Code Block
}

   catch (Exception err)
            {
                Session["StackTrace"] = err.StackTrace;
                Session["Message"] = err.Message;
                Session["Data"] = err.Data.ToString();
                Session["Source"] = err.Source;
                Session["ErrProcName"] = "Default_Page_Load";
                Response.Redirect("~/ErrorHandling/ShowError.aspx");

                //return "Process OK";
            }
0
Comment
Question by:Dovberman
  • 7
  • 4
11 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39593682
Do you get the timeout within Visual Studio, or only once deployed? Are you running any long-running operations anywhere?
0
 

Author Comment

by:Dovberman
ID: 39593710
The timeout occurs when deployed.  I am not sure if it occurs within Visual Studio.

Yes there are 3 long running operations within the code block. I need to find out which one is timing out.  The catch mode is not getting triggered.

I usually do not ask for my code to be reviewed.  However, I am stuck here. This is one of the 3 long operations.  Should I wrap the other 2 in a try catch block?

-------------
 try
            {
                SqlCommand cmdapdExceptions = new SqlCommand("usp_apdExceptions", conStockSelect);
                cmdapdExceptions.CommandType = System.Data.CommandType.StoredProcedure;
                cmdapdExceptions.CommandTimeout = 2400;

                // Append the Exceptions table
                for (int i = 0; i < intRow; i++)
                {
                    cmdapdExceptions.Parameters.Clear();
                    cmdapdExceptions.Parameters.Add("@SymbolID", SqlDbType.Int).Value = dblArrException[i, 0];
                    cmdapdExceptions.Parameters.Add("@Curr5DayAvg", SqlDbType.Float).Value = dblArrException[i, 1];
                    cmdapdExceptions.Parameters.Add("@Prev8DayAvg", SqlDbType.Float).Value = dblArrException[i, 2];


                    RunProcedure(cmdapdExceptions, "usp_apdExceptions");
                }  // End  for (int i = 0; i < intRow; i++)

                cmdapdExceptions.Dispose();

                // Delete from Exceptions WHERE (Curr5DayAvg - Prev8DayAvg) =0
                // OR PctChg >= 100

                strDelExceptionsSQL = "DELETE FROM Exceptions ";
                strDelExceptionsSQL += "WHERE (Curr5DayAvg - Prev8DayAvg) =0 OR (Curr5DayAvg - Prev8DayAvg)/(Prev8DayAvg) >= 1";

                SqlCommand cmdDelExceptions1 = new SqlCommand("DelExceptions", conStockSelect);
                cmdDelExceptions.CommandType = System.Data.CommandType.Text;
                cmdDelExceptions.CommandText = strDelExceptionsSQL;
                cmdDelExceptions.CommandTimeout = 2400;
                RunProcedure(cmdDelExceptions, "DelExceptions1");
                cmdDelExceptions.Dispose();

                // Execute query to update the PctChg

                string strUpdPctChgSQL = "UPDATE Exceptions ";
                strUpdPctChgSQL += "SET PctChg = 100 * (Curr5DayAvg - Prev8DayAvg)/(Prev8DayAvg) ";

                SqlCommand cmdUpdPctChg = new SqlCommand("UpdPctChg", conStockSelect);
                cmdUpdPctChg.CommandType = System.Data.CommandType.Text;
                cmdUpdPctChg.CommandText = strUpdPctChgSQL;
                cmdUpdPctChg.CommandTimeout = 2400;
                RunProcedure(cmdUpdPctChg, "UpdPctChg");
                cmdUpdPctChg.Dispose();

                // Get the MAX(PctChg)
                string strMaxPctChgSQL = "SELECT MAX(PctChg) As MaxPctChg FROM Exceptions";
                string strMaxPctChg = GetColValue(conStockSelect, strMaxPctChgSQL, "MaxPctChg", "String");
                double dblMaxPctChg = double.Parse(strMaxPctChg);

                // Get the ExceptionCount
                string strExceptionCountSQL = "SELECT Count(*) As ExceptionCount FROM Exceptions";
                string strExceptionCount = GetColValue(conStockSelect, strExceptionCountSQL, "ExceptionCount", "String");
                int intExceptionCount = int.Parse(strExceptionCount);

                if (conStockSelect.State == ConnectionState.Open)
                {
                    conStockSelect.Close();
                }

               
                // Update SiteURL and refDate
                //Fix
                string strUpdExceptionsSQL = "UPDATE Exceptions SET SiteURL = '" + ResearchSiteURL + "', refDate = '" + dteSampleABStart.ToShortDateString() + "'  ";
                SqlCommand cmdUpdExceptions = new SqlCommand("UpdExceptions", conStockSelect);
                cmdUpdExceptions.CommandType = System.Data.CommandType.Text;
                cmdUpdExceptions.CommandText = strUpdExceptionsSQL;
                cmdUpdExceptions.CommandTimeout = 2400;

                cmdUpdExceptions.Connection = conStockReduce;
                if (conStockReduce.State == ConnectionState.Closed)
                {
                    conStockReduce.Open();
                }
                RunProcedure(cmdUpdExceptions, "UpdExceptions");
                cmdUpdExceptions.Dispose();

                // Stop the clock
                DateTime endDT = DateTime.Now;
                TimeSpan ts = endDT.Subtract(startDT);
                lblExceptionStatus.Text = ts.Minutes.ToString() + " : " + ts.Seconds.ToString() + ": " + intExceptionCount.ToString() + " Exceptions Built";

                if (conStockSelect.State == ConnectionState.Open)
                {
                    conStockSelect.Close();
                }
            }

            catch (Exception err)
            {
                Session["StackTrace"] = err.StackTrace;
                Session["Message"] = err.Message;
                Session["Data"] = err.Data.ToString();
                Session["Source"] = err.Source;
                Session["ErrProcName"] = "Default_Page_Load";
                Response.Redirect("~/ErrorHandling/ShowError.aspx");

                //return "Process OK";
            }

        }
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39593715
Does the RunProcedure method have a try/catch?
0
 

Author Comment

by:Dovberman
ID: 39593719
This sounds like I need to wrap each RunProcedure in a try/catch.
Is that correct?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39593737
You said, "The catch mode is not getting triggered." I was wondering if RunProcedure had a try/catch that was handling the exception, hence you not seeing it within the method shown above.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Dovberman
ID: 39593750
I wrapped each RunProcedure.

The custom error page is not displayed.
Just this:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Debugging is on in web.config.
0
 

Author Comment

by:Dovberman
ID: 39593820
You are right. RunProcedure has a TryCatch.

I am wondering why the custom error page does not display.

 private string RunProcedure(SqlCommand cmd, string strProcName)
        {

            try
            {
                cmd.ExecuteNonQuery();
                return "Process OK";
            }
            catch (Exception err)
            {
                Session["StackTrace"] = err.StackTrace;
                Session["Message"] = err.Message;
                Session["Data"] = err.Data.ToString();
                Session["Source"] = err.Source;
                Session["ErrProcName"] = strProcName;
                Response.Redirect("~/ErrorHandling/ShowError.aspx");

                return "Process OK";
            }
        }
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
ID: 39593872
Add the following to your web.config *temporarily*. It will allow you to see details of the exception:

<system.web>  <!-- locate this section -->

    <customErrors mode="Off" />
...

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39593930
This was already in web.config

<customErrors
        mode="Off" defaultRedirect="~/ErrorHandling/ShowError.aspx">
    </customErrors>

I may need to debug the old fashioned way by commenting out blocks of code.
0
 

Author Comment

by:Dovberman
ID: 39596629
The issue is a stored procedure that returns no rows.
0
 

Author Closing Comment

by:Dovberman
ID: 39596631
The issue is a stored procedure that returns no rows.

Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now