Solved

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 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

13 Experts available now in Live!

Get 1:1 Help Now