Solved

error handling on my website

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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