-1

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.

Community
  • 1
  • 1
Alex
  • 3,730
  • 9
  • 43
  • 94
  • 1
    Possible duplicate of [What is the best way to determine a session variable is null or empty in C#?](http://stackoverflow.com/questions/234973/what-is-the-best-way-to-determine-a-session-variable-is-null-or-empty-in-c) – Quentin Roger Mar 30 '16 at 11:26
  • @QuentinRoger can you see my response to your duplicate comment, and if you agree remove the duplicate tag please – Alex Mar 30 '16 at 11:38
  • @Alex I'd argue the question already has an answer [here](http://stackoverflow.com/questions/6417902/checking-if-object-is-null-in-c-sharp) – ardila Mar 30 '16 at 12:04
  • @ardila can you explain how that is the case? The answer to my question relaties to the level of the variable to examine for a null value, if I apply the answer to the question you link to, using the variable in my original question, I wills till receive a null exception. Is what I am saying correct, or have I missed something? – Alex Mar 30 '16 at 12:19
  • @Alex knowing what object should be checked against null is not a good question as no canonical answer can exist for every program. Therefore knowing how to check for null *is* the canonical answer. Btw, your title should be "Checking if Session is null". Also of interest [What should I do if the current ASP.NET session is null?](http://stackoverflow.com/questions/1382791/what-should-i-do-if-the-current-asp-net-session-is-null/1382811#1382811), which includes how to check for a null `Session` object. – ardila Mar 30 '16 at 12:30
  • @ardila I will have to respectfully disagree - my answer below is not an opinion, it is the correct answer, and solved the problem. The answer was clearly that the session was null so it could not check a session variable. – Alex Mar 30 '16 at 12:54

2 Answers2

1

The problem was I wast trying to access a variable from a null session, so it could never check for the variable because the session was null.

The solution is as follows:

            if (HttpContext.Current.Session != null)
            {
                HttpContext.Current.Session[Constants.SESSION_USER] = user;
            }

This now checks the actual session, rather than the variable.

Alex
  • 3,730
  • 9
  • 43
  • 94
-2

try this:

if(!string.IsNullOrEmpty(HttpContext.Current.Session[Constants.SESSION_USER]))
{
//for session is available
}