[WebMethod(EnableSession = true)]
public static void PutIntoCardsList(string cardName)
{
var cards = (List<Card>)HttpContext.Current.Session["sessionVals"];
if (cards != null)
{
cards.Add(new Card {CardName = cardName});
}
else
{
cards = new List<Card> {new Card {CardName = cardName}};
}
HttpContext.Current.Session["sessionVals"] = cards;
}
Now you can access session by using Static WebMethod..
Good Luck !!..
Hi,
One of my project I need to use a Static WebMethod to set session.
But the compiler gives me an error like.
An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'
But if you still need to access session you may also try to use the following.
HttpContext.Current.Session["yourSessionVariable"];
and the following above your WebMethod
[WebMethod(EnableSession = true)]
Here in below I just copy and paste my working code.
No comments:
Post a Comment