Custom Error Pages in ASP.NET - Global.asax

B

Brian Munroe

Let me preface this post: This is my first web app in ASP.NET, I am
sure I did things very kludgy, so please bare with my possibly stupid
ASP.NET design.

I am trying to create a custom error page that starts the process of
logging the bug via a web form. I capture things like contact name,
time, date, etc plus the stack trace, error message and error source

The problem I am having is that everytime I cause an error, I keep
getting a generic System.Web.HttpUnhandledException message.

I configured the global.asax file like the following:

<script language="C#" runat="server">

protected void Application_Error(Object sender, EventArgs e) {
Exception lastException = Server.GetLastError();
if(lastException != null) {
Session["ErrorStack"] = lastException.StackTrace;
Session["ErrorSource"] = lastException.Source;
Session["ErrorMessage"] = lastException.Message;
}
Server.Transfer("error.aspx");
}
</script>

The event below is what I am testing on. SaveDailyEntryFormData2 is a
static method in a code-behind class. It basically inserts a new
record into a database and returns a string of "saved" if the record
was inserted correctly.

void Submit_Click(object sender, ImageClickEventArgs e) {
string yo = SaveDailyEntryFormData2(Request);
if (yo == "saved") {
Server.Transfer("daily.entry2.aspx");
} else {
Response.Write(yo);
}
}

I am artificially creating an error by passing an empty value in one of
the Request parameters, causing a poorly formed SQL query, and thus
causing an error message.

The standard ASP.NET error message page actually shows me the trace
inside of my code-behind and is actually very helpful in identifying
problems, but when I try and use my custom error code page, the
exception thrown is the very generic (HttpUnhandledException)

SaveDailyEntryFormData2 does not have any try/catch blocks and I am
wondering if that could be my problem?

thanks

-- brian
 
B

Brian Munroe

void Submit_Click(object sender, ImageClickEventArgs e) {
string yo = SaveDailyEntryFormData2(Request);
if (yo == "saved") {
Server.Transfer("daily.entry2.aspx");
} else {
Response.Write(yo);
}
}
SaveDailyEntryFormData2 does not have any try/catch blocks and I am
wondering if that could be my problem?

I just modified the code above to look like:

void Submit_Click(object sender, ImageClickEventArgs e) {
string yo = SaveDailyEntryFormData2(Request);
if (yo == "good") {
Server.Transfer("daily.entry2.aspx");
} else {
Session["ErrorStack"] = yo;
Server.Transfer("error.aspx");
}
}

I outfitted SaveDailyEntryFormData2() with a try/catch block where the
catch returns an Exception.ToString()

Don't know if that is correct, but it is getting closer to what I
want..I mean ideally I'd like to just be able to just use the stuff in
global.asax so I don't have to modify all my code (which is like 50
static methods and about 20 aspx pages)

Any comments/suggestions would be appreciated.

-- brian
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top