?
Solved

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

Posted on 2009-02-14
15
Medium Priority
?
1,549 Views
Last Modified: 2013-12-17
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
Comment
Question by:ziorrinfotech
  • 9
  • 6
15 Comments
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23642594
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
 

Author Comment

by:ziorrinfotech
ID: 23642610
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23642626
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23642636
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
 

Author Comment

by:ziorrinfotech
ID: 23642652
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23642670
Set your autoeventwireup to false and try it.
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23642678
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
 

Author Comment

by:ziorrinfotech
ID: 23642693
yes I tried application_error as welll but that is also not getting fired.
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23642705
Did you change your Page declaration after you pasted it in to a separate file?

<%@ Page language="c#" Codebehind="filename.cs" Inherits="classname" %>
0
 

Author Comment

by:ziorrinfotech
ID: 23642714
just tried it but no change.
Even i tried AutoEventWireup=false, then Page_load, init event were not getting fired
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23642719
Can you paste your page declaration and the first part of your page.cs file?
0
 

Author Comment

by:ziorrinfotech
ID: 23642726
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
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23642774
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
 

Author Comment

by:ziorrinfotech
ID: 23642801
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
 
LVL 15

Accepted Solution

by:
aibusinesssolutions earned 375 total points
ID: 23642818
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

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