0

In an MVC project, trying to access one of the session variables in the View page.

var command = new SqlCommand(query, conn);
conn.Open();
Session["username"] = "swe12387";
command.ExecuteNonQuery();

In the above case, the value of the Session variable is as expected.

But in the below case, the session variable is null.

var command = new SqlCommand(query, conn);
conn.Open();
command.ExecuteNonQuery();
Session["username"] = "swe12387";

What would the problem be? Could running a query be making any change in the session state? Thanks in advance.

Gokul
  • 788
  • 2
  • 12
  • 30
  • 3
    In your second example, is there an exception thrown when calling `command.ExecuteNonQuery();`? Basically there is no obvious reason why `Session["username"]` should not contain `swe12387` other than, maybe, that line having not actually been executed. – Jason Evans Apr 28 '16 at 10:46

1 Answers1

0

I would second Jason on that. Is this a long running query? Check the Session ID before and after the call, whether it has changed.

Are there any services running in the background. You might be Abandoning or disposing the session, or removing the session variable somewhere somewhere you might have forgotten about.

sameerfair
  • 406
  • 7
  • 13