请大家帮我解决一下这个问题,这是我写的的代码:
string strConnection=System.Configuration.ConfigurationSettings.AppSettings["connectionstring"].ToString();
SqlConnection conn=new SqlConnection(strConnection);
                            conn.Open();
string uname=Session["uname"].ToString();
Label1.Text="select sname from zhengming where sname=uname"
                            close.Close();
调试的时候没有报错啊,可是就是Label的Text没有得到值啊.请问这要如何解决啊.

解决方案 »

  1.   

    有数据啊,这是出现的错误:
    未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 40:  SqlConnection conn=new SqlConnection(strConnection);
    行 41:  conn.Open();
    行 42:  string uname=Session["uname"].ToString().Trim();
    行 43:  Label1.Text="select sname from zhengming where sname=uname";
    行 44:  Label2.Text="select ssno from zhengming where sname=uname";
     
      

  2.   

    标签里你只是给它一个SQL语句,并没有负值啊,而且连数据库应该不能只用一个SqlConnection吧
    至少也要有个SqlCommand或DataSet和DataReader,先把数据读出来再传值,我是这么用的,请高手指点
      

  3.   

    1.最主要原因:SQL语句赋值给了label.Text,而没有在数据库中执行,应用SqlCommand执行并返回结果。
    2.sql语句构造也有问题:string.Format("select sname from zhengming where sname={0}",uname)可以返回正确的SQL语句。
      

  4.   

    出现错误:有数据啊,这是出现的错误:
    未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 40:  SqlConnection conn=new SqlConnection(strConnection);
    行 41:  conn.Open();
    行 42:  string uname=Session["uname"].ToString().Trim();
    行 43:  Label1.Text="select sname from zhengming where sname=uname";
    行 44:  Label2.Text="select ssno from zhengming where sname=uname";这个的原因是因为你没有对Session["uname"]进行NULL判断就直接对其进行了Session["uname"].ToString().Trim();操作,如果Session["uname"]为NULL,就会引发这个异常
    至于为什么Label的Text没有得到值,如果有值的话唯一的可能是"select sname from zhengming where sname=uname"因为你没有建立SqlCommand对象用来执行SQL语句
      

  5.   

    你的SQL语句直接作为文本传给了LABEL1,而没有被执行,
      

  6.   

    我已经对Session作了判断,但是在调试的时候,却说NULL在该页面中不存在。
      

  7.   

    应该是这样判断:if (Session["uname"] != null)
    注意是null不是NULL,这个是我偷懒写成了NULL,不好意思
      

  8.   

    这个我改过来了,但是我在判断之前加了一句代码,主要是用来调试,Response.Write("sdjk")
    可是这一句根本就淌有执行啊,当然后面就更不用说了
      

  9.   

    就你现在提供的代码来看,我们不大可能了解到为什么Response.Write("sdjk")没有执行
      

  10.   

    if(Session["uname"].ToString()==null)
    {
    Response.Redirect("chaxun.aspx");
    }
    else
    {
    string uname=Session["uname"].ToString();
    string strConnection=System.Configuration.ConfigurationSettings.AppSettings["connectionstring"].ToString();
    SqlConnection conn=new SqlConnection(strConnection);
    conn.Open();
    SqlCommand cmd=new SqlCommand();
    cmd.CommandType=CommandType.Text;
    cmd.CommandText="select sname from zhengming sname=uname";
    cmd.Connection=conn;
    cmd.ExecuteNonQuery();
    Label1.Text=cmd.CommandText;
      

  11.   

    这些代码都是写在page_load事件里的
      

  12.   

    if(Session["uname"].ToString()==null)
    先改成
    if(Session["uname"] ==null)
      

  13.   

    我调试的时候把这个Session改成空了的,但是Respons.Write("chaxun.aspx")这一句就没有执行啊,也不知道是怎么一回事
      

  14.   

    if(Session["uname"].ToString()==null)
    {
    Response.Redirect("chaxun.aspx");
    }
    你改为空的话Session["uname"].ToString()就为""而不是null
    所以跳转到else