C# – Session is null when calling a web service in ASP.NET C#


I have a Login Class which has a function: isCorrect() that takes username and password as two attributes And a asp.net WebService To allow using AJAX.


public Login CorrectLogin(string username, string password) 
   Login thisLogin = Login.isCorrect(username, password);
   int thisLoggedinUserID = thisLogin.LoggedinUserID;

   if (thisLoggedinUserID != 0)
      Session["loggedinUser"] = thisLoggedinUserID;

   return thisLogin;

When I want to set value of Session["loggedinUser"] = thisLoggedinUserID this error accrues:

Object reference not set to an instance of an object.

I can't understand what is solution.

Best Solution

Web services don't have Session by default. Add an attribute to the WebMethod..

 public Login CurrentLogin .....
Related Question