• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 924
  • Last Modified:

Server.GetLastError is Nothing on custom error page

I'm trying to write a custom error page, to display the error message within a page matching the "house style". I thought that I could use Server.GetLastError to get the exception that was thrown, but it has a value of "Nothing".

In my web.config I have:

    <customErrors mode="On"  defaultRedirect="error.aspx" />

And in the code-behind of error.aspx I have:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim ex As Exception = Server.GetLastError   ' <--- ex is Nothing at this point
        lblError.Text = ex.Message

    End Sub

This is making my custom error page pretty useless. What am I doing wrong?
2 Solutions
The defaultRedirect occurs AFTER the server has processed all the error junk, if you want to get the last error, you either have to do it in Page_Error or Application_Error

Maybe i am missing something but i have the following code in the pack of my page, but the sub Page_error never gets called when the error page gets loaded. 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()
    End Sub

Open in new window

Gyanendra SinghArchitectCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now