Page_Error event issue

S

Sam Solomon

Dear All,

I am catching a error in my Page_Error event and storing it in a session
variable. After the error is generated the page is redirected to another page
where I want to show complete error details in a label in the Page load event
of the directed page. But the problem is that my session variable seems to be
empty. Below is my code


ErrorPage="WebForm2.aspx" attribute is already set for the following page
which redirects it to WebForm2.aspx


private void Page_Error(object sender,System.EventArgs e)

{

Session["Error"]= Server.GetLastError();
Server.ClearError();

}

In the WebForm2.aspx load event I have :



private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

Label1.Text="Error" + "<br>" ;

if(Session["Error"]!=null)

{

Exception ex = (Exception)Session["Error"];
Label1.Text+= ex.ToString();}

}

}

In my Web.Config file customErrorMode is set to "On"

When the error is generated its does get redirected to WebForm2. But I
cannt see the Error message in my label.

Any help would be greatly appreciated.

cheers,

Sam Solomon
 
P

Phillip Williams

Hi Sam,

Your Page_Error method is not being called because you have probably not
wired up an event handler for it in the InitializeComponent method:
this.Page.Error +=new EventHandler(Page_Error);

I could tell that it was not called because if it had, the
Server.ClearError() step that you called would have prevent the redirect to
webform2.aspx.

You mixed 2 ways of handling the error together. Here are the 2 ways:

1- Turn the customErrors="off", remove the tag ErrorPage="WebForm2.aspx" and
add an event handler to Page.Error, in which case you method should be
modified like this:
private void Page_Error(object sender,System.EventArgs e)

{

Session["Error"]= Server.GetLastError();
Server.ClearError();
Response.Redirect("webform2.aspx");
}

2- Keep the customErrors and the ErrorPage tags as they are, remove the
Page_Error handling method and modify the Page_load method of webform2.aspx
to look like this:
private void Page_Load(object sender, System.EventArgs e)

{

Label1.Text="Error" + "<br>" ;
if(Session["Error"]!=null)
{

Label1.Text+= Server.GetLastError().ToString();
Server.ClearError();
}
 
P

Phillip Williams

One correction, the last function should not check for the session variable
anymore:
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text="Error" + "<br>" ;
Label1.Text+= Server.GetLastError().ToString();
Server.ClearError();
}

Phillip Williams said:
Hi Sam,

Your Page_Error method is not being called because you have probably not
wired up an event handler for it in the InitializeComponent method:
this.Page.Error +=new EventHandler(Page_Error);

I could tell that it was not called because if it had, the
Server.ClearError() step that you called would have prevent the redirect to
webform2.aspx.

You mixed 2 ways of handling the error together. Here are the 2 ways:

1- Turn the customErrors="off", remove the tag ErrorPage="WebForm2.aspx" and
add an event handler to Page.Error, in which case you method should be
modified like this:
private void Page_Error(object sender,System.EventArgs e)

{

Session["Error"]= Server.GetLastError();
Server.ClearError();
Response.Redirect("webform2.aspx");
}

2- Keep the customErrors and the ErrorPage tags as they are, remove the
Page_Error handling method and modify the Page_load method of webform2.aspx
to look like this:
private void Page_Load(object sender, System.EventArgs e)

{

Label1.Text="Error" + "<br>" ;
if(Session["Error"]!=null)
{

Label1.Text+= Server.GetLastError().ToString();
Server.ClearError();
}

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Sam Solomon said:
Dear All,

I am catching a error in my Page_Error event and storing it in a session
variable. After the error is generated the page is redirected to another page
where I want to show complete error details in a label in the Page load event
of the directed page. But the problem is that my session variable seems to be
empty. Below is my code


ErrorPage="WebForm2.aspx" attribute is already set for the following page
which redirects it to WebForm2.aspx


private void Page_Error(object sender,System.EventArgs e)

{

Session["Error"]= Server.GetLastError();
Server.ClearError();

}

In the WebForm2.aspx load event I have :



private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

Label1.Text="Error" + "<br>" ;

if(Session["Error"]!=null)

{

Exception ex = (Exception)Session["Error"];
Label1.Text+= ex.ToString();}

}

}

In my Web.Config file customErrorMode is set to "On"

When the error is generated its does get redirected to WebForm2. But I
cannt see the Error message in my label.

Any help would be greatly appreciated.

cheers,

Sam Solomon
 
K

Keith Patrick

Wouldn't AutoEventWireUp=true in the @Page directive result in Page_Error
getting called without that manual initialization?
 
K

Keith Patrick

Guess it's yet another bit of template behavior out of VS that violates
coding guidelines. I am growing weary of going through all my auto-gen'ed
code (at least on the build I'm on, it defaults to "true" in direct
contradiction to the documentation) and cleaning up things that one team on
MS decides to put into code while another team decides on practices counter
to that. I have over 80 FXCop errors in my ASP.Net app related to
automatically-generated code, including the name of the basic webform when
you create it (default.aspx contains the name of a language keyword)!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top