Link to home
Start Free TrialLog in
Avatar of ziorrinfotech
ziorrinfotech

asked on

Page_Error event does not get when c# code is in separte file

I am have two web form and one of web form has c# code in a separate file and second have in the same file.
When I add the code given below in to the second page then it is working fine. I am getting the error information as Page_error event gets fired.

but  when I paste the same code on web form 1 in a separate code file then it does not work.
could any one tell me how can i use Page_error event when i write code in a separate code file

<script language=C# runat="server">
void Page_Load(object sender, System.EventArgs e)
{
	throw(new ArgumentNullException());
}
 
public void Page_Error(object sender,EventArgs e)
{
	Exception objErr = Server.GetLastError().GetBaseException();
	string err =	"<b>Error Caught in Page_Error event</b><hr><br>" + 
			"<br><b>Error in: </b>" + Request.Url.ToString() +
			"<br><b>Error Message: </b>" + objErr.Message.ToString()+
			"<br><b>Stack Trace:</b><br>" + 
	                  objErr.StackTrace.ToString();
	Response.Write(err.ToString());
	Server.ClearError();
}
</script>

Open in new window

Avatar of aibusinesssolutions
aibusinesssolutions
Flag of United States of America image

Since C# does not automatically wire up the events for you, you will need to add a handler for it.


this.Error += new System.EventHandler (this.Page_Error );
Avatar of ziorrinfotech
ziorrinfotech

ASKER

Hi aibusinesssolutions

Thanks for the help,
I have AutoEventWireup="true" in aspx page then also will it my code not work ??

secondly
I tried the code which you gave me,
 protected void Page_Load(object sender, EventArgs e)
    {
        this.Error += new System.EventHandler(this.Page_Error);      
        throw (new ArgumentException("Directory name cannot contain the "));

    }

but  then also the no change Page_error event does not fired.

help me here pls
AutoEventWireup does not work with C#, only VB.

All AutoEventWireup does is add "Handles" to the end of functions, which C# can not use, so you have to wire everything up yourself.

But anyways, you need to move the init to the Page_Init function.

protected void Page_Init(object sender, EventArgs e)
 {
    this.Page.Error += new System.EventHandler(this.Page_Error);
 }
If you want that to be global error catching, you might be better off adding it to your global.asax file in the Application_Error sub

protected void Application_Error(object sender, EventArgs e)
{
      Exception objErr = Server.GetLastError().GetBaseException();
      string err =      "Error Caught in Application_Error event\n" +
                  "Error in: " + Request.Url.ToString() +
                  "\nError Message:" + objErr.Message.ToString() +
                  "\nStack Trace:" + objErr.StackTrace.ToString();
      EventLog.WriteEntry("Sample_WebApp",err,EventLogEntryType.Error);
      //Server.ClearError();
      //additional actions...
}
Hi aibusinesssolutions


protected void Page_Init(object sender, EventArgs e)
 {
    this.Page.Error += new System.EventHandler(this.Page_Error);
 }

this also does not work, same old result.
Set your autoeventwireup to false and try it.
Actually, after a little more research, it appears that the Page_Error is supposed to be wired up for you when you have autoeventwireup set to true, so I'm not sure why it isn't firing.

Did you try putting it in your application_error function?
yes I tried application_error as welll but that is also not getting fired.
Did you change your Page declaration after you pasted it in to a separate file?

<%@ Page language="c#" Codebehind="filename.cs" Inherits="classname" %>
just tried it but no change.
Even i tried AutoEventWireup=false, then Page_load, init event were not getting fired
Can you paste your page declaration and the first part of your page.cs file?
sure
here is my page.aspx 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>Untitled Page</title>
    </head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
    </form>
</body>
</html>

=======================================================================================

here is .cs code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PageEvent : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        this.Page.Error += new System.EventHandler(this.Page_Error);

    }
    protected void Page_Load(object sender, EventArgs e)
    {
       
        throw (new ArgumentException("Directory name cannot contain the "));


    }
 

    public void Page_Error(object sender, EventArgs e)
    {
        Exception objErr = Server.GetLastError().GetBaseException();
        string err = "<b>Error Caught in Page_Error event</b><hr><br>" +
                "<br><b>Error in: </b>" + Request.Url.ToString() +
                "<br><b>Error Message: </b>" + objErr.Message.ToString() +
                "<br><b>Stack Trace:</b><br>" +
                          objErr.StackTrace.ToString();
        Response.Write(err.ToString());
        Server.ClearError();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        throw (new ArgumentException("Directory name cannot contain the "));

    }
}


I just copied your code, and took out the page_init function to let it wire it up itself, and it worked.

public partial class PageEvent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            throw (new ArgumentException("Directory name cannot contain the "));
        }

        public void Page_Error(object sender, EventArgs e)
        {
            Exception objErr = Server.GetLastError().GetBaseException();
            string err = "<b>Error Caught in Page_Error event</b><hr><br>" +
                    "<br><b>Error in: </b>" + Request.Url.ToString() +
                    "<br><b>Error Message: </b>" + objErr.Message.ToString() +
                    "<br><b>Stack Trace:</b><br>" +
                              objErr.StackTrace.ToString();
            Response.Write(err.ToString());
            Server.ClearError();
        }      
    }
I don't know why it is not working on my pc.

I did the same thing which you mention and does not work for me
ASKER CERTIFIED SOLUTION
Avatar of aibusinesssolutions
aibusinesssolutions
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial