我在一个页做一个登陆(login.aspx.cs)   protected string User_Login(string user, string pass)
    {
     string myssss;
        if (user == "" || pass == "")
        {
            return "用户名和密码不能为空!";
        }
        try 
        {
            using (SqlConnection conn = DBClass.GetConnection("KnetWork_sysdb"))
            {
                conn.Open();
                int result = -1;
                string mywps = FormsAuthentication.HashPasswordForStoringInConfigFile(pass.ToUpper(),"MD5");
                SqlCommand sqlcomm = new SqlCommand("checkAdminLogin", conn);
                sqlcomm.CommandType = CommandType.StoredProcedure;                sqlcomm.Parameters.Add("@StaffCard_StaffName", SqlDbType.NVarChar, 50).Value = user;
                sqlcomm.Parameters.Add("@StaffPassword", SqlDbType.NVarChar, 50).Value = mywps;
                sqlcomm.Parameters.Add("@K_LogIP", SqlDbType.VarChar, 40).Value = Request.UserHostAddress.ToString();                sqlcomm.Parameters.Add("@StaffNo", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Output;
                sqlcomm.Parameters.Add("@K_LogIPout", SqlDbType.NVarChar, 40).Direction = ParameterDirection.Output;
                sqlcomm.Parameters.Add("@K_LastLoginDate", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Output;
                sqlcomm.Parameters.Add("return1", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                sqlcomm.ExecuteNonQuery();                result = (int)sqlcomm.Parameters["return1"].Value;                switch (result)
                {
                    case 0:                        this.Session["KNet_Admin_AdminName"] = user; //卡号或是用名
                        this.Session["KNet_Admin_AdminPassword"] = mywps;                        this.Session["KNet_Admin_StaffNo"] = sqlcomm.Parameters["@StaffNo"].Value.ToString();
                        this.Session["KNet_Admin_K_LogIPout"] = sqlcomm.Parameters["@K_LogIPout"].Value.ToString();
                        this.Session["KNet_Admin_K_LastLoginDate"] = sqlcomm.Parameters["@K_LastLoginDate"].Value.ToString();
                        myssss="登录成功";
=============================================
上面登陆已成功.其中我还测试了保存的Session中的值也有了,this.Session["KNet_Admin_AdminName"]
this.Session["KNet_Admin_AdminPassword"]
this.Session["KNet_Admin_K_LogIPout"] 
this.Session["KNet_Admin_K_LastLoginDate"]
this.Session["KNet_Admin_StaffNo"]这四个Session中都有了值,问题就出现在我在一个类中要获取这几个Session的值,就是获取不到。
这个类大底如下:
类名:loginM.cs
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;using KNet.DBUtility;
using KNet.Common;namespace KNet.Common
{
    /// <summary>
    /// AdminloginMess 登陆处理的摘要说明
    /// </summary>
    public class AdminloginMess
    {  
        private string StaffNo;            //唯一号(Session)
        private string StaffCard;           //卡号(Session)
        private string StaffName;           //姓名
        private string K_LogIP;             //上次登陆IP地址
        private string K_LastLoginDate;     //上次登陆时间
        //****************获取相关信息***************************************
        /// <summary>
        /// 获取登陆后台的用户的相关信息
        /// </summary>
        public AdminloginMess()
        {
this.StaffNo =(string)System.Web.HttpContext.Current.Session["KNet_Admin_StaffNo"];
        this.StaffCard = "admin66";  
  红的这个总是取不到值,报错下如:
未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 56:                 this.StaffNo = System.Web.HttpContext.Current.Session["KNet_Admin_StaffNo"].ToString(); 
===========
哪位高人知道是什么回事吗?我想知在一个页登陆后,保存Session值,在一个类(.cn)中如何获取这个Session值?

解决方案 »

  1.   

    如果这个类是在 Virtual Webservice 中使用,你将那不到。其他情况应该能拿到。
      

  2.   

    真的是取不到,不知什么原因,我现在又写了一个类,然后实例化总是出错。这个类如下:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Data.Sql;
    using KNet.DBUtility;
    using KNet.Common;
    /// <summary>
    /// Class1 的摘要说明
    /// </summary>
    namespace KNet.Common
    {
        public class Class1
        {
            private string StaffNo;            //唯一号(Session)
            public Class1()
            {
                this.StaffNo = System.Web.HttpContext.Current.Session["KNet_Admin_StaffNo"].ToString();
            }        public string Knet_StaffNotest
            {
                get
                {
                    return StaffNo;
                }
            }
        }}=================
    实例后引用的页:
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;using KNet.DBUtility;
    using KNet.Common;
    using KNet.Business;
    public partial class Admin_hame : System.Web.UI.Page
    {    AdminloginMess AM = new AdminloginMess();
        Class1 C1 = new Class1();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                Label1.Text =AM.Knet_CompanyName.ToString();            AM.Add_Logs("登陆成功");
                Response.Write("<Hr>");
                Response.Write("ReadProductsSQL:");
                Response.Write("<Hr>");
                Response.Write(Session["KNet_Admin_StaffNo"]);
                Response.Write("sssss");
                Response.Write("<Hr>s");
                Response.Write(C1.Knet_StaffNotest);
               
            }
        }}
    ===================
    报错如下:未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 23:         public Class1()
    行 24:         {
    行 25:             this.StaffNo = System.Web.HttpContext.Current.Session["KNet_Admin_StaffNo"].ToString();
    行 26:         }
    行 27: 
     
      

  3.   

    你这个类在哪的啊?不是在app_code里的吧?那肯定取不到的啊.....
      

  4.   

    session是web的东西
    不要放在类库里
    放在aspx.cs就能取了
    你可以在pageload里取
    然后再传给你的loginM.cs