Solved

error handling on my website

Posted on 2011-03-04
1
299 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
1 Comment
 
LVL 35

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

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