?
Solved

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

Posted on 2013-01-25
4
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

777 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