SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=127.0.0.1;Initial Catalog=bookstore;Integrated Security=True";
        con.Open();        SqlCommand cmd = new SqlCommand();
        string sql;
        sql = string.Format("select * from bookinfo where bname='{0}'", Session["searchresult"] );
        cmd.Connection = con;
        cmd.CommandText = sql;        object o = cmd.ExecuteScalar();        if (o == null)
        {
            Response.Write("无结果");
        }
        else
        {
            Response.Write("有结果");
        }
        con.Close();        Label1.Text = Session["searchresult"].ToString() ;-------->这一行提示"未将对象引用设置到对象的实例"。

解决方案 »

  1.   

    Session["searchresult"] 的值是null
      

  2.   

    1楼是结果页面的代码.
    这个是前一个页面搜索按钮的代码,现在我改在前面点击的时候来执行了.
    如果把执行全部放在搜索按钮里面.
    第二个页面只执行加载gridview,这样可以么???
    session还是没有赋值?        SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=127.0.0.1;Initial Catalog=bookstore;Integrated Security=True";
            con.Open();        SqlCommand cmd = new SqlCommand();
            string sql;
            sql = string.Format("select * from bookinfo where bname='{0}'", TextBox3.Text.Trim() );
            cmd.Connection = con;
            cmd.CommandText = sql;        object o = cmd.ExecuteScalar();        if (o == null)
            {
                Session["searchrestul"] = "no";
            }
            else
            {
                Session["searchrestul"] = TextBox3.Text.Trim();
            }
            
            con.Close();        Response.Redirect("searched.aspx");
      

  3.   


    if(Session["searchresult"]!=null)
    {
    Label1.Text = Session["searchresult"].ToString() 
    }
      

  4.   

    Session["searchresult"] 空的 进行。ToString()转换时报错。 加个判断
     if(Session["searchresult"]!= null)
    {
      赋值。
    }