Solved

How to wrap an aspx.cs code page in an error handler.

Posted on 2013-01-25
4
334 Views
Last Modified: 2013-01-26
A customer is experiencing an exception error when selecting a menu item that opens a page.

I would like to place an error handler to detect an exception in the page load event.

How can I do this?

 protected void Page_Load(object sender, EventArgs e)
    {
                Code here, works on my machine, but not from user's machine.

                 // Call the error handler if an exception occurs.
                // ErrorHandler("mycode", "Page Load")

    }

Here is my error handler:

private string ErrorHandler(string CodeLine, string strProcName)
    {
        try
        {
             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";
        }
    }

Thanks,
0
Comment
Question by:Dovberman
  • 2
  • 2
4 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 38820672
So if the error is due to the issue in the code in your Page_Load then wrap your page load code inside try catch

Else handle Application level Error event in the global.aspx.
0
 

Author Comment

by:Dovberman
ID: 38820914
How do I wrap the Pge Load code?

  protected void Page_Load(object sender, EventArgs e)

    {
   
        //Get the connection string

        System.Configuration.Configuration rootWebConfig =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/StockPro");

        if (0 < rootWebConfig.ConnectionStrings.ConnectionStrings.Count)
        {
            connString =
                rootWebConfig.ConnectionStrings.ConnectionStrings["ConStockSelect"];
            if (null != connString)
                Console.WriteLine("StockPro connection string = \"{0}\"",
                    connString.ConnectionString);
            else
                Console.WriteLine("No StockPro connection string");
        }

} // end of page load code

// Where do I put the try, catch statements?
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 38820919
protected void Page_Load(object sender, EventArgs e)

    {
       try{
        //Get the connection string

        System.Configuration.Configuration rootWebConfig =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/StockPro");

        if (0 < rootWebConfig.ConnectionStrings.ConnectionStrings.Count)
        {
            connString =
                rootWebConfig.ConnectionStrings.ConnectionStrings["ConStockSelect"];
            if (null != connString)
                Console.WriteLine("StockPro connection string = \"{0}\"",
                    connString.ConnectionString);
            else
                Console.WriteLine("No StockPro connection string");
        }
}
catch(Exception ex)
{
   //You have exception details in ex... you can do anything you like with ex 
            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");
}

} // end of page load code

Open in new window

0
 

Author Closing Comment

by:Dovberman
ID: 38821654
Thank you.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

803 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