Solved

error handling on my website

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

628 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