0

i just want to show error details with getlasterror object.

here is my web config code to redirect custom error page :

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

and here is my Error.aspx page code to catch error :

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class Error : newManageClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadError(Server.GetLastError());
        }
    }
    protected void LoadError(Exception objError)
    {
        if (objError != null)
        {
            StringBuilder lasterror = new StringBuilder();
            if (objError.Source != null)
            {
                //lasterror.AppendLine("Source:");
                //lasterror.AppendLine(objError.Source);
                //lasterror.AppendLine();
                lbl_from_page.Text = objError.Source.ToString();
            }
            if (objError.Message != null)
            {
                //lasterror.AppendLine("Message:");
                //lasterror.AppendLine(objError.Message);
                //lasterror.AppendLine();
                lbl_errormessage.Text = objError.Message.ToString();
            }
            //if (objError.InnerException != null)
            //{
            //    lasterror.AppendLine("StackTrace:");
            //    lasterror.AppendLine(objError.StackTrace);
            //    lasterror.AppendLine();
            //    lbl_error_desc.Text = objError.InnerException.ToString();
            //}
            ViewState.Add("LastError", lasterror.ToString());
        }
    }
}

how ever i can not get details from which page with full url and error message.

please help me...

Shal
  • 309
  • 4
  • 7
  • 25

2 Answers2

0

You are performing a redirect, which means you are losing the context from your initial request, thus you are unable to access that exception. Add this to your web.config to perform a rewrite instead:

<customErrors mode="On" defaultRedirect="Error.aspx" redirectMode="ResponseRewrite" />
Mister Epic
  • 15,703
  • 11
  • 68
  • 126
0

You should add attribute redirectMode="ResponseRewrite" to ensure that Server.GetLastError() will not return null.

<customErrors mode="On" defaultRedirect="Error.aspx" redirectMode="ResponseRewrite"/>

Information about source page (where error occurred) can be then found in Request.Url in case of redirectMode="ResponseRewrite" otherwise it can be found in url parameter ?aspxerrorpath=........

More info is here ASP.NET custom error page - Server.GetLastError() is null

Community
  • 1
  • 1
Jaroslav Kubacek
  • 1,287
  • 16
  • 24