I'm currently working on a series of webforms that when clicked through in the right order, information is stored into a Session. If a user goes to this page directly I want to redirect them back to the first webform page. I can get this
When I run this code locally on my machine, the Session is null and the if/else statement works, redirecting to default.aspx. However when I load the files to the server and run from the webpage I get a Object reference not set to an instance of an object.
error.
protected void Page_Load(object sender, EventArgs e)
{
if (Session != null && Session["ReasonForAdminRights"] != null)
{
ll.Text = Session["ReasonForAdminRights"].ToString();
}
else
{
Response.Redirect("~/default.aspx");
}
}
I've tried a number of variations without any luck, including:
if (!string.IsNullOrEmpty(Session["ReasonForAdminRights"] as string))
{
ll.Text = ((string)Session["ReasonForAdminRights"]);
}
And
if (Session["ReasonForAdminRights"] != null)
I've done some Googling and the answers on Stackoverflow have told me that the session object isn't correctly set. But I'm stuck as to how to debug it further on the server? I don't know how to find out what's actually in there, as it should be null so should always go to the else statement.
Any help would be appreciated! I'm at a bit of a loss!
Answers tried:
What does "Object reference not set to an instance of an object" mean?
What is a NullReferenceException, and how do I fix it?
Object reference not set to an instance of an object.
EDIT Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ServiceDeskHelpForms.user_details_collection.Page_Load(Object sender, EventArgs e) in \\homes\My Documents\Visual Studio 2015\Projects\SDHelpForms\SDHelpForms\user-details-collection.aspx.cs:24
System.Web.UI.Control.OnLoad(EventArgs e) +108
System.Web.UI.Control.LoadRecursive() +67
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+4497