Solved

error handling on my website

Posted on 2011-03-04
1
303 Views
Last Modified: 2012-05-11
I am using .net framework 4.0 with c#.
I want to auto redirect to a page when any uncaught exception occurs on my web pages.
I introduced an error in the key name on the web.config file to test this functionality.

When I run this in the debugger I get the following error:
"ArgumentNullException was unhandled by user code"

on the following line: throw (new ArgumentNullException());        
Please review the following and suggest corrections.

Thanks.


I added this code to my web.config:
<system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <customErrors defaultRedirect="PageEvent.aspx" mode="On">
    </customErrors>
...
</system.web>

Open in new window


Then I added a web form named PageEvent.aspx to my website.

The PageEvent.aspx has the following code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageEvent.aspx.cs" Inherits="PageEvent" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div class = "Error">
        <asp:Label ID="Label1" runat="server" Text="Error Caught in Page_Error event."></asp:Label>
        <asp:Label ID="_ErrIn" runat="server"></asp:Label>
        <asp:Label ID="_ErrMessage" runat="server"></asp:Label>
        <asp:Label ID="_StackTrace" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

Open in new window


The PageEvent.aspx.cs has the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PageEvent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        throw (new ArgumentNullException());        
    }

    public void Page_Error(object sender, EventArgs e)
    {
        Exception objErr = Server.GetLastError().GetBaseException();
        string ErrIN = "Error in: " + Request.Url.ToString();
        string ErrMessage = "Error Message: " + objErr.Message.ToString();
        string StackTrc = "Stack Trace: " + objErr.StackTrace.ToString();
        Server.ClearError();
        //show error on screen
        _ErrIn.Text = Session["ErrIn"].ToString();
        _ErrMessage.Text = Session["ErrMessage"].ToString();
        _StackTrace.Text = Session["StackTrc"].ToString(); 
    }

}

Open in new window




0
Comment
Question by:patd1
[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
1 Comment
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 35040881
Check:
http://support.microsoft.com/kb/306355
and follow  the steps described at: "How to use the Web.config file"
the problem of your page is that we do not use page_error to surface the exception. just use page_load
protected void Page_Load(object sender, EventArgs e)
    {
        Exception objErr = Server.GetLastError().GetBaseException();
//fill up your labels here
    }

0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

759 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