Solved

error handling on my website

Posted on 2011-03-04
1
301 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hey!!!! 1 22
SQL Help 27 45
Call Controller Action Method from ASPX 2 15
Need help deploying my first MVC.Net app with a SQL Server backend 3 13
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

810 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