我菜鸟一个,正在学习阶段,做的模拟网站。出现问题了大家看看。
public string jibie;
    protected void Page_Load(object sender, EventArgs e)
    {
        string name = Request.Cookies["username"].ToString();
        string strSql = "select jibie from user_region where DengLuYongHuMing='"+name+"'";
        jibie = Education.DBUtility.DbHelperSQL.GetSingle(strSql).ToString();
     
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (jibie == "招生宝高级会员" || jibie == "终极会员")
        {
            Response.Redirect("zsbFB.aspx");
        }
        else
        {
            Response.Redirect("peixunkechengFB.aspx");
        }
    }
运行后出现的错误
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 18:         string name = Request.Cookies["username"].ToString();
行 19:         string strSql = "select jibie from user_region where DengLuYongHuMing='"+name+"'";
行 20:         jibie = Education.DBUtility.DbHelperSQL.GetSingle(strSql).ToString();
行 21:      
行 22:     }
 
源文件: d:\My Documents\Visual Studio 2005\Projects\WebUI\huiyuan\Controls\Tree.ascx.cs    行: 20 

解决方案 »

  1.   

    不能调试无法确定故障的准确原因,但我猜是在这个位置:
    GetSingle(strSql)
    请单独检查这个的返回结果。
      

  2.   

    string name = Request.Cookies["username"].ToString();
            string strSql = "select jibie from user_region where DengLuYongHuMing='"+name+"'";
            jibie = Education.DBUtility.DbHelperSQL.GetSingle(strSql).ToString();
        将这里的代码更改成: 
    try{
    string name = Request.Cookies["username"].ToString();
            string strSql = "select jibie from user_region where DengLuYongHuMing='"+name+"'";
            jibie = Education.DBUtility.DbHelperSQL.GetSingle(strSql).ToString();
        }
    catch(Exception exp)
    {
            System.Diagnostics.Debug.WriteLine(exp.Message);
    }在输出窗口就可以看到错误信息了。
      

  3.   

    我想是cookies这里
    if (Request.Cookies["username"]!=null)
      string name = Request.Cookies["username"].ToString(); 
      

  4.   

    Request.Cookies["username"]有值吗?单步调下试试
      

  5.   

    加个if先判断一下Cookies是否有值