6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.Web.Security;
using System.Data;
using System.IO;
using SurelyKnown.Core;
using System.Configuration;
using System.Collections;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml;
using System.Windows.Forms;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [System.Web.Services.WebMethod(EnableSession = true)] 
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        int newOrgID = Convert.ToInt32(Session["uOrgID"].ToString());

The error is on the last line

 CS0120: An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'

WHat should I do to get the session value inside the method.

Mark
  • 2,720
  • 15
  • 56
  • 87
  • Possible duplicate of http://stackoverflow.com/questions/5586564/asp-net-access-session-from-static-method-static-class – christofr Oct 17 '11 at 12:34

2 Answers2

10

Use HttpContext.Current.Session

int newOrgID=0;
if(HttpContext.Current.Session["uOrgID"]!=null)
{
  int.TryParse(HttpContext.Current.Session["uOrgID"].ToString(),out newOrgID);
}
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • @Mark - The Session is page property and it cannot be used outside the Page class or in static method so you have get reference of Session object from the context via HttpContext.Current.Session method. – KV Prajapati Oct 17 '11 at 12:43
0

check for null before using it, something like this:

if(Session["uOrgID"] != null)
{
  int newOrgID = Convert.ToInt32(Session["uOrgID"].ToString());
}

You should also read this article to really understand how to access Session state from web services (including web and page methods): Using ASP.NET Session State in a Web Service

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • That's actually not a good practice. Session["uOrgID"] can be removed before you get a chance to parse it. You should do string uOrgID = Session["uOrgID"] as string; if(!string.IsNullOrEmpty(uOrgID)) .... – Icarus Oct 17 '11 at 13:02