• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1561
  • Last Modified:

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

0
ziorrinfotech
Asked:
ziorrinfotech
  • 9
  • 6
1 Solution
 
aibusinesssolutionsCommented:
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 );
0
 
ziorrinfotechAuthor Commented:
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
0
 
aibusinesssolutionsCommented:
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);
 }
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
aibusinesssolutionsCommented:
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...
}
0
 
ziorrinfotechAuthor Commented:
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.
0
 
aibusinesssolutionsCommented:
Set your autoeventwireup to false and try it.
0
 
aibusinesssolutionsCommented:
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?
0
 
ziorrinfotechAuthor Commented:
yes I tried application_error as welll but that is also not getting fired.
0
 
aibusinesssolutionsCommented:
Did you change your Page declaration after you pasted it in to a separate file?

<%@ Page language="c#" Codebehind="filename.cs" Inherits="classname" %>
0
 
ziorrinfotechAuthor Commented:
just tried it but no change.
Even i tried AutoEventWireup=false, then Page_load, init event were not getting fired
0
 
aibusinesssolutionsCommented:
Can you paste your page declaration and the first part of your page.cs file?
0
 
ziorrinfotechAuthor Commented:
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 "));

    }
}


0
 
aibusinesssolutionsCommented:
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();
        }      
    }
0
 
ziorrinfotechAuthor Commented:
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
0
 
aibusinesssolutionsCommented:
Here are the 3 files for my project if you want to try them out.  Just remove the .txt from the end.
Default.aspx.txt
Default.aspx.vb.txt
web.config.txt
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now