public static string selVal;//设置查询条件
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Convert.ToBoolean(Session["Role"]))
                selVal = "";
            else
            {
                selVal = " where UserID=" + Session["UserID"].ToString();
               
            }
            DataListBind();
        }
    }
调试结果为:未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 22:             else
行 23:             {
行 24:                 selVal = " where UserID=" + Session["UserID"].ToString();
行 25:                
行 26:             }
 

解决方案 »

  1.   

        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
                if( (Session["Role"]) != null )
                {
                  if (Convert.ToBoolean(Session["Role"])) 
                      selVal = ""; 
                  else 
                  {
                      if( (Session["UserID"]) == null )
                        selVal = "";
                      else
                        selVal = " where UserID=" + Session["UserID"].ToString(); 
                  } 
                }
                DataListBind(); 
            }
        }