2011-10-17 74 views
4
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()); 

的错误是最后一行获取方法

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

我应该怎么做才能在方法内部会话值内Session变量。

+0

可能的重复http://stackoverflow.com/questions/5586564/asp-net-access-session-from-static-method-static-cl屁股 – christofr 2011-10-17 12:34:49

回答

8

使用HttpContext.Current.Session

int newOrgID=0; 
if(HttpContext.Current.Session["uOrgID"]!=null) 
{ 
    int.TryParse(HttpContext.Current.Session["uOrgID"].ToString(),out newOrgID); 
} 
+0

如何使用它。你可以在这里添加代码 – Mark 2011-10-17 12:36:43

+0

@Mark - 会话是页面属性,它不能在页面类或静态方法外使用,所以你通过HttpContext.Current.Session方法从上下文中获取Session对象的引用。 – adatapost 2011-10-17 12:43:35

0

检查null在使用它之前,这样的事情:

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

你也应该阅读这篇文章,以真正了解如何从Web服务访问会话状态(包括网页和页面方法):Using ASP.NET Session State in a Web Service