Link to home
Start Free TrialLog in
Avatar of jimseiwert
jimseiwertFlag for United States of America

asked on

ASP.NET Error Handler

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
ASKER CERTIFIED SOLUTION
Avatar of Gyanendra Singh
Gyanendra Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
also if you are create yr method in class so u need to use this
 HttpContext.Current
Avatar of jimseiwert

ASKER

this is what i got
error.bmp
please dont put this on page load event . it is not correct ... check this post for details

https://www.experts-exchange.com/questions/21134839/Server-GetLastError-is-Nothing-on-custom-error-page.html
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

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
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 ?
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