Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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