I need to store a collection of strings in session, and I have a web method that I'm calling through a jQuery ajax call:
[WebMethod]
public string AddIdToSession(string userId)
{
List<string> userIds = new List<string>();
if (HttpContext.Current.Session != null &&
HttpContext.Current.Session["userIds"] != null)
{
userIds = (List<string>)Session["userIds"];
userIds.Add(userId);
HttpContext.Current.Session["userIds"] = userIds;
}
else
{
userIds.Add(userId);
HttpContext.Current.Session.Add("userIds", userIds); // error here
}
return userId;
}
I'm getting an error when I try to add the id to session:
Object reference not set to an instance of an object.
Am I doing this wrong?