CustomErrors Trapping in a Page -

S

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
 
M

MattC

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
 
S

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


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
 
B

Brock Allen

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.
 
M

MattC

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
 
S

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

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
 
M

MattC

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
 

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

Similar Threads

CustomErrors Trapping in a Page 1
customErrors 1
<customErrors 3
CustomErrors! What is going on? 0
customErrors 1
encrypted connectionStrings web.config customErrors 2
customErrors 0
CustomErrors for a 404 code 2

Members online

No members online now.

Forum statistics

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

Latest Threads

Top