472,119 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

CustomErrors Trapping in a Page -

SMG
Hi ,

Sory for incomplete message in last post here is the actual problem..

I am using following code in web.confiig for trapping all the error through
out my site..

<customErrors mode="On" defaultRedirect="WebForm1.aspx">
<error statusCode="404" redirect="ServerError.aspx"></error>
<error statusCode="500" redirect="WebForm3.aspx"></error>
</customErrors>

It works very fine... but On the final page e.g WebForm3.aspx I want catch
the error and want to show it on the screen.
so i wrote following code in webform3.aspx but it throws an error

// Create an Exception object from the Last error
//that occurred on the server
Exception myError =Server.GetLastError();

// Get the error message
string str = "Error Message :" + myError.Message;

It doesn't work it gives an error at following line
strErrorMsg += "\n\nError Message :" + myError.Message;

Regards,
Shailesh Gajare
Nov 19 '05 #1
6 1376
What error is it giving? Do you have any error handling in your
global.asax.(vb/cs).

Place try..catch block around GetLasterError to see what exception is being
thrown.

MattC
"SMG" <SM*@nodmain.com> wrote in message
news:eB**************@TK2MSFTNGP10.phx.gbl...
Hi ,

Sory for incomplete message in last post here is the actual problem..

I am using following code in web.confiig for trapping all the error
through
out my site..

<customErrors mode="On" defaultRedirect="WebForm1.aspx">
<error statusCode="404" redirect="ServerError.aspx"></error>
<error statusCode="500" redirect="WebForm3.aspx"></error>
</customErrors>

It works very fine... but On the final page e.g WebForm3.aspx I want catch
the error and want to show it on the screen.
so i wrote following code in webform3.aspx but it throws an error

// Create an Exception object from the Last error
//that occurred on the server
Exception myError =Server.GetLastError();

// Get the error message
string str = "Error Message :" + myError.Message;

It doesn't work it gives an error at following line
strErrorMsg += "\n\nError Message :" + myError.Message;

Regards,
Shailesh Gajare

Nov 19 '05 #2
SMG
Hi MattC,

It says:::
System.NullReferenceException: Object reference not set to an instance of
an object. "error is at the star(*) "

Exception myError =Server.GetLastError();
*string str = "Error Message :" + myError.Message;
My mode of customErrors is Off so that it shows the error on the screen

Objective : If we get any error on any of the pages through out the site,
then it should show a single customize page where
the error is explained and a email is shooted at one id.
Regards,
Shailesh Gajare
"MattC" <m@m.com> wrote in message
news:eI**************@TK2MSFTNGP15.phx.gbl...
What error is it giving? Do you have any error handling in your
global.asax.(vb/cs).

Place try..catch block around GetLasterError to see what exception is being
thrown.

MattC
"SMG" <SM*@nodmain.com> wrote in message
news:eB**************@TK2MSFTNGP10.phx.gbl...
Hi ,

Sory for incomplete message in last post here is the actual problem..

I am using following code in web.confiig for trapping all the error
through
out my site..

<customErrors mode="On" defaultRedirect="WebForm1.aspx">
<error statusCode="404" redirect="ServerError.aspx"></error>
<error statusCode="500" redirect="WebForm3.aspx"></error>
</customErrors>

It works very fine... but On the final page e.g WebForm3.aspx I want catch
the error and want to show it on the screen.
so i wrote following code in webform3.aspx but it throws an error

// Create an Exception object from the Last error
//that occurred on the server
Exception myError =Server.GetLastError();

// Get the error message
string str = "Error Message :" + myError.Message;

It doesn't work it gives an error at following line
strErrorMsg += "\n\nError Message :" + myError.Message;

Regards,
Shailesh Gajare

Nov 19 '05 #3
The problem is that using the customErrors causes a redirect to the server.
The redirect is a completely ew request into the server and thus the prior
error information is gone. The customErrors is a great way to automatically
redirect the client to a user-friendly error page.

Now, I suspect you want to capture the error information. This should be
done in global.asax in Application_Error. This event is raised by ASP.NET
to notify you that there was an unhandled expcetion. In here is where you
can call Server.GetLastError. Once you have the error, do whatever you need
to with it like log it to the EventLog a log file, email it to an admin,
whatever.

Now just in case you reply asking how you can show the error on the error
page, I'd say you shouldn't. Never show error information to clients 1) for
user friendliness reasons and 2) security reasons.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Hi ,

Sory for incomplete message in last post here is the actual problem..

I am using following code in web.confiig for trapping all the error
through out my site..

<customErrors mode="On" defaultRedirect="WebForm1.aspx">
<error statusCode="404" redirect="ServerError.aspx"></error>
<error statusCode="500" redirect="WebForm3.aspx"></error>
</customErrors>
It works very fine... but On the final page e.g WebForm3.aspx I want
catch
the error and want to show it on the screen.
so i wrote following code in webform3.aspx but it throws an error
// Create an Exception object from the Last error
//that occurred on the server
Exception myError =Server.GetLastError();
// Get the error message
string str = "Error Message :" + myError.Message;
It doesn't work it gives an error at following line
strErrorMsg += "\n\nError Message :" + myError.Message;
Regards,
Shailesh Gajare


Nov 19 '05 #4
What you should do then is capture the error in the Application_OnError
method in Global.asax. Handle you excecption here and place whatever you
need in the Session object. Server.ClearError then transfer the user to your
custom page. Here you can pull out you details from the Session/send emails
etc.

Alternatively just log the error in the event log. Then set customErrors
mode to RemotrOnly and redirect to your nice friendly page.

MattC
"SMG" <SM*@nodmain.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi MattC,

It says:::
System.NullReferenceException: Object reference not set to an instance of
an object. "error is at the star(*) "

Exception myError =Server.GetLastError();
*string str = "Error Message :" + myError.Message;
My mode of customErrors is Off so that it shows the error on the screen

Objective : If we get any error on any of the pages through out the site,
then it should show a single customize page where
the error is explained and a email is shooted at one id.
Regards,
Shailesh Gajare
"MattC" <m@m.com> wrote in message
news:eI**************@TK2MSFTNGP15.phx.gbl...
What error is it giving? Do you have any error handling in your
global.asax.(vb/cs).

Place try..catch block around GetLasterError to see what exception is
being
thrown.

MattC
"SMG" <SM*@nodmain.com> wrote in message
news:eB**************@TK2MSFTNGP10.phx.gbl...
Hi ,

Sory for incomplete message in last post here is the actual problem..

I am using following code in web.confiig for trapping all the error
through
out my site..

<customErrors mode="On" defaultRedirect="WebForm1.aspx">
<error statusCode="404" redirect="ServerError.aspx"></error>
<error statusCode="500" redirect="WebForm3.aspx"></error>
</customErrors>

It works very fine... but On the final page e.g WebForm3.aspx I want
catch
the error and want to show it on the screen.
so i wrote following code in webform3.aspx but it throws an error

// Create an Exception object from the Last error
//that occurred on the server
Exception myError =Server.GetLastError();

// Get the error message
string str = "Error Message :" + myError.Message;

It doesn't work it gives an error at following line
strErrorMsg += "\n\nError Message :" + myError.Message;

Regards,
Shailesh Gajare


Nov 19 '05 #5
SMG
Thanks Mattc,
That's a good idea. But this the way we used to do in ASP what is the best
way we can do it in ASP.Net
One more I am allowed to use session then how do I show the error on the
next page, If I pass it through a querystring that is not a good way...
then what are other options I have.

Can I use Context.Handler or something like this...

One more I am trying to avoid writing the even log entry or writing in a log
file as if there is any problem in the global.asax this will halt my entire
application

What is Industry standard/ best practices for this?

Thanks once again
Shailesh G

"MattC" <m@m.com> wrote in message
news:ub**************@TK2MSFTNGP09.phx.gbl...
What you should do then is capture the error in the Application_OnError
method in Global.asax. Handle you excecption here and place whatever you
need in the Session object. Server.ClearError then transfer the user to your
custom page. Here you can pull out you details from the Session/send emails
etc.

Alternatively just log the error in the event log. Then set customErrors
mode to RemotrOnly and redirect to your nice friendly page.

MattC
"SMG" <SM*@nodmain.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi MattC,

It says:::
System.NullReferenceException: Object reference not set to an instance of
an object. "error is at the star(*) "

Exception myError =Server.GetLastError();
*string str = "Error Message :" + myError.Message;
My mode of customErrors is Off so that it shows the error on the screen

Objective : If we get any error on any of the pages through out the site,
then it should show a single customize page where
the error is explained and a email is shooted at one id.
Regards,
Shailesh Gajare
"MattC" <m@m.com> wrote in message
news:eI**************@TK2MSFTNGP15.phx.gbl...
What error is it giving? Do you have any error handling in your
global.asax.(vb/cs).

Place try..catch block around GetLasterError to see what exception is
being
thrown.

MattC
"SMG" <SM*@nodmain.com> wrote in message
news:eB**************@TK2MSFTNGP10.phx.gbl...
Hi ,

Sory for incomplete message in last post here is the actual problem..

I am using following code in web.confiig for trapping all the error
through
out my site..

<customErrors mode="On" defaultRedirect="WebForm1.aspx">
<error statusCode="404" redirect="ServerError.aspx"></error>
<error statusCode="500" redirect="WebForm3.aspx"></error>
</customErrors>

It works very fine... but On the final page e.g WebForm3.aspx I want
catch
the error and want to show it on the screen.
so i wrote following code in webform3.aspx but it throws an error

// Create an Exception object from the Last error
//that occurred on the server
Exception myError =Server.GetLastError();

// Get the error message
string str = "Error Message :" + myError.Message;

It doesn't work it gives an error at following line
strErrorMsg += "\n\nError Message :" + myError.Message;

Regards,
Shailesh Gajare


Nov 19 '05 #6
You'll find that writing to the event log in the global.asax is one of most
common ways to document errors in an app.

try something along the lines of:

Global.asax.cs
protected void Application_Error(Object sender, EventArgs e)
{
//wrtite to event log
Session["LastError"] = SomeMethodToHandleError(Server.GetLastError());
}

web.config
<customErrors defaultRedirect="Error.aspx" mode="RemoteOnly" />
Error.aspx.cs

private void Page_Load(object sender, System.EventArgs e)
{
_myErrorLabel.Text = Session["LastError"].ToString();
}
MattC

"SMG" <SM*@nodmain.com> wrote in message
news:eX**************@TK2MSFTNGP15.phx.gbl...
Thanks Mattc,
That's a good idea. But this the way we used to do in ASP what is the best
way we can do it in ASP.Net
One more I am allowed to use session then how do I show the error on the
next page, If I pass it through a querystring that is not a good way...
then what are other options I have.

Can I use Context.Handler or something like this...

One more I am trying to avoid writing the even log entry or writing in a
log
file as if there is any problem in the global.asax this will halt my
entire
application

What is Industry standard/ best practices for this?

Thanks once again
Shailesh G

"MattC" <m@m.com> wrote in message
news:ub**************@TK2MSFTNGP09.phx.gbl...
What you should do then is capture the error in the Application_OnError
method in Global.asax. Handle you excecption here and place whatever you
need in the Session object. Server.ClearError then transfer the user to
your
custom page. Here you can pull out you details from the Session/send
emails
etc.

Alternatively just log the error in the event log. Then set customErrors
mode to RemotrOnly and redirect to your nice friendly page.

MattC
"SMG" <SM*@nodmain.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi MattC,

It says:::
System.NullReferenceException: Object reference not set to an instance of
an object. "error is at the star(*) "

Exception myError =Server.GetLastError();
*string str = "Error Message :" + myError.Message;
My mode of customErrors is Off so that it shows the error on the screen

Objective : If we get any error on any of the pages through out the site,
then it should show a single customize page where
the error is explained and a email is shooted at one id.
Regards,
Shailesh Gajare
"MattC" <m@m.com> wrote in message
news:eI**************@TK2MSFTNGP15.phx.gbl...
What error is it giving? Do you have any error handling in your
global.asax.(vb/cs).

Place try..catch block around GetLasterError to see what exception is
being
thrown.

MattC
"SMG" <SM*@nodmain.com> wrote in message
news:eB**************@TK2MSFTNGP10.phx.gbl...
Hi ,

Sory for incomplete message in last post here is the actual problem..

I am using following code in web.confiig for trapping all the error
through
out my site..

<customErrors mode="On" defaultRedirect="WebForm1.aspx">
<error statusCode="404" redirect="ServerError.aspx"></error>
<error statusCode="500" redirect="WebForm3.aspx"></error>
</customErrors>

It works very fine... but On the final page e.g WebForm3.aspx I want
catch
the error and want to show it on the screen.
so i wrote following code in webform3.aspx but it throws an error

// Create an Exception object from the Last error
//that occurred on the server
Exception myError =Server.GetLastError();

// Get the error message
string str = "Error Message :" + myError.Message;

It doesn't work it gives an error at following line
strErrorMsg += "\n\nError Message :" + myError.Message;

Regards,
Shailesh Gajare



Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Matthias S. | last post: by
1 post views Thread by SMG | last post: by
reply views Thread by Michel Lapointe | last post: by
reply views Thread by John Clark | last post: by
2 posts views Thread by Fred Nelson | last post: by
9 posts views Thread by =?Utf-8?B?TWlrZQ==?= | last post: by
1 post views Thread by JJ | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.