Home All Groups Group Topic Archive Search About
Author
15 Nov 2005 4:01 PM
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

Author
15 Nov 2005 5:28 PM
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();
}

Show quoteHide quote
"Sam Solomon" wrote:

> 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
>
Author
15 Nov 2005 5:35 PM
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();
}

Show quoteHide quote
"Phillip Williams" wrote:

> 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" wrote:
>
> > 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
> >
Author
16 Nov 2005 3:57 PM
Keith Patrick
Wouldn't AutoEventWireUp=true in the @Page directive result in Page_Error
getting called without that manual initialization?
Author
16 Nov 2005 8:31 PM
Phillip Williams
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftips.asp

"Avoid the Autoeventwireup Feature
Instead of relying on autoeventwireup, override the events from Page. For
example, instead of writing a Page_Load() method, try overloading the public
void OnLoad() method. This allows the run time from having to do a
CreateDelegate() for every page."

--
[note: if this post answers your question, you can mark it as an answer
using the web-based newsreader functions]
-----
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Show quoteHide quote
"Keith Patrick" wrote:

> Wouldn't AutoEventWireUp=true in the @Page directive result in Page_Error
> getting called without that manual initialization?
>
>
>
Author
16 Nov 2005 10:18 PM
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)!