Application level error handling in ASP .Net

Application level error handling can be done in  2 ways:

1.Add an Application_Error event handler to the global asax file for your web application
2.Add a <customErrors> element in web.config file for your web application.

1.Application_Error Event handler(Using first way):

 Code in aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="page1].aspx.cs" Inherits="page1_" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
//btn1 click event and we want to transer to page welcome.aspx which
//is does not exists but we want to check error so i assumed that i taken
//welcome.aspx which is not in application you can specify any other file name
//which is not in your applciaiotn
    protected void btn1_click(object s, EventArgs e)
    {
        Server.Transfer("welcome.aspx");
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>GLOBAL ASAX</title>
</head>
<body>
    <form id="form1" runat="server">
taken a button on which when client clicks
    <asp:Button ID="btn1" runat="server" Text="Click here to check" OnClick="btn1_click" />
    </form>
</body>
</html>

After that add Global.asax file and place code showing below:

 <%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
    //Application_Error event handler to handle the error in web application
//and placed code to get error message
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs
	//Server.GetLastError().Message gets detailed error message and we can also
	//short the error by using Server.ClearError() function to get only
	//Error message not detailed error mesage
        Response.Write(Server.GetLastError().Message);
        Server.ClearError();
    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }
       
</script>

2.<customErrors element in web.config file(Second way):

Code in aspx page will be same as shown above in aspx file....

now add web.config file in your application and place code shown below:
 <?xml version="1.0"?>
<configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
		<!--in customerros element there is a attribute which have 3 mode-->
		<!--first is OFF which it shows a standard micrsoft asp .net error page will be shown when a error occures-->
		<!--Second is RemoteOnly in that case custom error page will not apply to visitors logged on locally
		at the web server itself.and its useful for developers as well for administrator for troubleshooting-->
		<!--In defaultRedirect attribute you specify custom error page that will display when any error occures i taken
		Error.htm so that if any error occure this htm page get invoked-->
		<customErrors defaultRedirect="Error.htm" mode="On"/>
        <compilation debug="false" />
        <authentication mode="Windows" />
    </system.web>
</configuration>

Code in HTML file for Custom Error
 <!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>
    <title>Error on Page</title>
</head>
<body bgcolor="white">
<font color="red">Error while processing</font>
</body>
</html>

this html page get shown in case of error





NOTE:
 If you add both web.config as well Global.asax file then Global.aspx will handle your error not web.config
Ebook Download
View all
Learn
View all