0

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

Community
  • 1
  • 1
hlh3406
  • 1,230
  • 4
  • 25
  • 41
  • Are you sure the null exception comes from the page in question, and not from the page where you get redirected? Could it be `ll` that is null? – user1429080 Jan 25 '17 at 14:56
  • @user1429080 ll is a label within my front end page - but surely I shouldn't hit it? The Session should be null so I should automatically redirect? Or have I misunderstood! :) – hlh3406 Jan 25 '17 at 15:03
  • Can you post stack trace – Saravanan Jan 25 '17 at 15:05
  • @Saravanan yes - put it into the question – hlh3406 Jan 25 '17 at 15:09
  • What if you remove the line containing `ll`. Obviously your label won't get set, but that would tell you whether the object null exception is really coming from session being null or not. – Craig H Jan 25 '17 at 15:11
  • `Session` should likely not be null. `Session["ReasonForAdminRights"]` should probably be null unless you have put something there before. If you are sure it steps into `if` block, then `ll` seems to be the only possible candidate for being null. **Why** that would be null is an interesting question. You could try to do `ll.Text = "test";` just before the `if` and see if the line number in the stack trace changes. – user1429080 Jan 25 '17 at 15:12
  • @CraigH removed the ll.text and the error still shows on the line `Session["ReasonForAdminRights"].ToString();` – hlh3406 Jan 25 '17 at 15:17
  • @user1429080 tried that and the number didn't change - but the error showed on this line instead: `ll.Text = "tester";` Not sure what that means? – hlh3406 Jan 25 '17 at 15:20
  • Tried removing everything from inside the if statement but I still get the same error on the same line, now it's just inside the if statement – hlh3406 Jan 25 '17 at 15:22
  • 1
    I believe you have a broken installation. When you have been testing this, how exactly have you been moving you changes from your dev env to the server? Which files have you copied etc? Try to wipe everything from the server, then re-deploy the complete application... – user1429080 Jan 25 '17 at 15:29
  • @user1429080 That was it! thank you so much for your help! If you put that into an answer I'll mark it as correct. Thank you! – hlh3406 Jan 25 '17 at 15:32

2 Answers2

2

Note: Answer based on commment trail on question

The problem is likely caused by a broken installation on the server. A complete wipe + redeploy on the server may fix the problem.

user1429080
  • 8,816
  • 3
  • 25
  • 47
0
var sessionValue= (string)(Session["ReasonForAdminRights"]);
if(!string.IsNullOrEmpty(sessionValue))
{
//do something...
}
  • Tried this - but it still errors on this line: `ll.Text = ((string)Session["ReasonForAdminRights"]);` Even though it shouldn't be getting into the if statement as the Session should be null. – hlh3406 Jan 25 '17 at 15:05