Solved

ASP.NET Error Handler

Posted on 2008-09-30
8
203 Views
Last Modified: 2011-10-19
I have a .net web application and in my config file i have

<customErrors mode="On" defaultRedirect="~/error.aspx" />
Then on the error page I use Server.GetLastError.ToString which is not working it returns "Object reference not set to an instance of an object."

How do i get the error the redirected the user to the default error page so i can correct it.

Thank you
0
Comment
Question by:jimseiwert
  • 5
  • 3
8 Comments
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 500 total points
ID: 22607168
try this
Sub Page_Error(sender as Object, e as EventArgs)

  dim PageException as string = Server.GetLastError().ToString()
  dim strBuild as new StringBuilder()
  strBuild.Append("Exception!")
  strBuild.Append(PageException)

  Response.Write(strBuild.ToString())
  Context.ClearError()

End Sub
0
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22607190
also if you are create yr method in class so u need to use this
 HttpContext.Current
0
 
LVL 2

Author Comment

by:jimseiwert
ID: 22607484
this is what i got
error.bmp
0
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22607605
please dont put this on page load event . it is not correct ... check this post for details

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_21134839.html
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:jimseiwert
ID: 22607674
Ops, i just posted my comment on the wrong thread. I added the below code and placed a break point on the sub you told be to add but it never gets called on my generic error page that the web.config redirects it to. What am i missing?

Thank you for your patience
Imports System.Web

Imports System.Web.SessionState

Imports System.Web.Mail

Imports System.Diagnostics

Partial Class Sessions

    Inherits System.Web.UI.Page
 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 

    End Sub

    Sub Page_Error(ByVal sender As Object, ByVal e As EventArgs)
 

        Dim PageException As String = Server.GetLastError().ToString()

        Dim strBuild As New StringBuilder()

        strBuild.Append("Exception!")

        strBuild.Append(PageException)
 

        Response.Write(strBuild.ToString())

        Context.ClearError()
 

    End Sub

Open in new window

0
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22607739
see that event called when some error comes in your page ,, nor that page is loaded .. try to throw some error on page laod and see
0
 
LVL 2

Author Comment

by:jimseiwert
ID: 22607774
but this is the generic page that the web.config file calls when the application has an error. If i force an error then the server,getlasterror will retun the wrong error wont it ?
0
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22611631
see this Page_Error error call when that page has error , for application related error you need to write code in global.asax in application_error event
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now