1

I'm hardly to figure out how to solve this.. I want to catch the error when the server is turn off and redirect the page to Maintenance.aspx. But it's getting error at HttpContext.Current.Application["ErrorCode"].ToString();.Please help me how to solve this...

Many thanks.

try{
// method here
}
 catch (Exception ex)
       {
            Panel1.Visible = true;
            string statuscode = HttpContext.Current.Application["ErrorCode"].ToString(); //Getting error here!
            if (statuscode != null || statuscode != string.Empty)
            {
                if (statuscode == "500")
                {
                    lblDetailMsg.Text = "<b>Error Page- <b> " + HttpContext.Current.Application["ErrorPage"].ToString() + " <br /> <b>Error Message-</b> The Requested Page was not found.";
                    Response.Redirect("Maintenance.aspx");
                }
            }

        }

1 Answer 1

1

If I understand your code right, then the error must be due to .toString() method.

Try this

string statuscode = Convert.ToString(HttpContext.Current.Application["ErrorCode"]); // added Convert.Tostring()
    if (statuscode != null || statuscode != string.Empty)
    {
        if (statuscode == "500")
        {
             lblDetailMsg.Text = "<b>Error Page- <b> " + Convert.ToString(HttpContext.Current.Application["ErrorPage"]) + " <br /> <b>Error Message-</b> The Requested Page was not found.";
             Response.Redirect("Maintenance.aspx");
        }
    }

Convert.ToString() handles null, while ToString() doesn't.

Here it seems that HttpContext.Current.Application["ErrorCode"] does not contain value for "Error Code", so it giving null value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.