Solved

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

Posted on 2013-01-25
4
336 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Input parameteres to DragOver 2 34
SSRS Deployment problem 5 91
Install IIS7.5 on Windows Sever 2012 R2 4 31
Nested forach loop to linq 3 24
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 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