I am trying to create an error message page to display an exception when an exception occurs, and there is a return button on the error message page to return to the previous page where the exception was thrown.
here is the code used to redirect the error page.
protected void btnAssign_Click(object sender, EventArgs e)
{
try
{
SqlDataSource3.Insert();
}
catch (Exception ex)
{
Session["Exception"] = ex;
Response.Redirect("~/ErrorMessage.aspx", false);
}
}
here is the code for my global.asax file
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError().InnerException;
Session["Exception"] = ex;
Response.Redirect("~/ErrorMessage.aspx");
}
here is the errorMessage page code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Exception ex = (Exception)Session["Exception"];
Session.Remove("Exception");
Literal1.Text = "<p style='color:blue'><b>An unrecoverable error has occurred:</b></p><br /><p style='color:red'>" + ex.Message + "</p>";
}
}
protected void btnReturn_Click(object sender, EventArgs e)
{
Response.Redirect("~/IncidentAssignment.aspx");
}
When I click the Assign button, it opens the errorMessage page and displays an exception, but when I click the return button, the program crashed and pointed to the global.asax file and reported that the session state was not available in this context, as shown in the hollow.

, [ "exception" ] Null. , - . .