Gui
Gui

Reputation: 9813

Check if session is null is returning error

I'm using jQuery to get a few values and them i send to an Webservice. On that WebService i'm checking if a session is null but i'm receiving Null Reference to that session and it gets error (i see it on javascript console, on debugging i just see the error on output "A first chance exception of type 'System.NullReferenceException' occurred in PromotorDeSaude.DLL".

I'm checking if session is null like this:

if (HttpContext.Current.Session["encomenda"] != null)

Am i doing something wrong?

Upvotes: 0

Views: 2128

Answers (2)

Andreas Paulsson
Andreas Paulsson

Reputation: 7813

To access Session in a web service method, add the attribute

[WebMethod(EnableSession = true)]

to the method. See http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx for more information.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502816

That suggests that HttpContext.Current or HttpContext.Current.Session may be null - which would happen if you're running on a different thread, for example.

I suggest you log every bit of the expression to find out which bit is null, and work from there.

Upvotes: 1

Related Questions