I have the following line of code:
HttpContext.Current.Session[Constants.SESSION_USER] = user;
Which is causing an exception, I assume because the session is null.
If I remove this line, the exception does not get thrown.
I am able to retrieve the ID from the user
object, so I assume that is not what is causing the problem.
From this question:
How to know if a session has been set
I tried the following:
if (HttpContext.Current.Session[Constants.SESSION_USER] == null)
throw new Exception("SESSIONULL");
else
throw new Exception("SESSIONOTNULL");
But neither of these exceptions get thrown - I assume because it is trying to access a null object so throws before it gets to execute my code.
If so, how can I tell if the session is null, I know I can use a try catch block as this stops the error, but I would prefer to resolve this in a more precise manner.
any ideas?
EDIT: this was not a duplicate of the question above, as the question above is trying to access a variable from within the session, I needed to check if the actual session was null. Se my answer below.