放了个textbox,想运行程序后能在这个textbox里显示出统计次数的结果的,但是一无所获。sql代码在数据库里执行的时候是能成功统计出结果的。。求解原因
protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        SqlConnection cn = new SqlConnection("server=localhost;database=jtgl;user=sa;password=sa");
        SqlCommand cmd = new SqlCommand("select count(bltype) from ajd where bltype=1", cn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "ajd");
        this.TextBox1.Text = ds.Tables[0].Rows[0][0].ToString(); 
    }

解决方案 »

  1.   

    select count(bltype) from ajd where bltype='1' 最后要加单引号的吧 
      

  2.   

    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>SqlConnection cn = new SqlConnection("server=(local);database=jtgl;user=sa;password=sa");
      

  3.   

    SqlConnection cn = new SqlConnection("Data Source==(local);Database=jtgl;Uid=sa;Pwd=sa");
      

  4.   

    既然你的SQL是可以直接得到正确结果的,那么bltype是整形是确定无误的了吧。
    那么LZ确定你的SqlConnection 连接串写对了吗?
      

  5.   

     SqlDataAdapter da = new SqlDataAdapter(cmd,cn);
      

  6.   

    看错了。那里没问题。server=.;uid=sa;pwd=sa;database=jtgl;
      

  7.   

    SqlConnection cn = new SqlConnection("server=.;database=jtgl;uid=sa;pwd=sa");
      SqlCommand cmd = new SqlCommand("select count(*) from ajd where bltype=1", cn);this.TextBox1.Text = cmd.ExecuteScalar().ToString()
      

  8.   

    select count(bltype) as ct from ajd where bltype=1
      

  9.   

    看了下。
    1、cn.Open();
    2、cmd.ExecuteScalar()
    楼主去看下基础吧。。数据库没有打开如何执行呢。
    返回第一行第一列使用:SqlCommand 的ExecuteScalar方法就行了。何必那么麻烦呢。
      

  10.   

    DataAdapter不需要手动打开数据库吧?
      

  11.   

    sql代码在数据库里执行的时候是能成功统计出结果.
    .
    .
    ....那么应该是你赋值给textbox出问题了,断点调试下.
      

  12.   

    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>最重要是你没有自动回传吧 如果调试数据库链接 绑定没有问题那肯定是这里的问题了
      

  13.   

     设置回执属性  AutoPostBack=True 要不然textbox无法自动发送
      

  14.   

    SqlDataAdapter  不需要显式打开吧 
      

  15.   

    TextChanged事件好像是当textbox改变时才执行吧,直接使用好像不好使吧,为什么不加个button呢,点击button直接给textbox赋值不就行了吗
      

  16.   

    求个单值至于用DATASET么?ExecuteScalar多简单。
    例子:
    SqlConnection cn = new SqlConnection("server=(local);database=northwind;uid=sa;pwd=123");
                SqlCommand cmd = cn.CreateCommand();
                string str = "select count(*) from orders";
                cmd.CommandText = str;
                cn.Open();
                string sum = cmd.ExecuteScalar().ToString();
                cn.Close();
                textBox1.Text = sum;另外补充一下,使用SqlDataAdapter 不需要显式打开,你原本打开的,它会保持打开,你原本关闭的,它会自动打开,然后在自动关闭。
      

  17.   

    楼主,你SqlConnection Open了吗?你不Open怎么能行???
      

  18.   


    建议你去看看sqlcommand和sqldataadapter的两种写法,应该为
      SqlConnection cn = new SqlConnection("server=localhost;database=jtgl;user=sa;password=sa");
      //SqlCommand cmd = new SqlCommand("select count(bltype) from ajd where bltype=1",cn);
      SqlDataAdapter da = new SqlDataAdapter("select count(bltype) from ajd where bltype=1",cn);
      DataSet ds = new DataSet();
      da.Fill(ds, "ajd");
      this.TextBox1.Text = ds.Tables[0].Rows[0][0].ToString()
      

  19.   

    哦,还落下了cn.Open();
     SqlConnection cn = new SqlConnection("server=localhost;database=jtgl;user=sa;password=sa");
    cn.Open();  
    //SqlCommand cmd = new SqlCommand("select count(bltype) from ajd where bltype=1",cn);
    SqlDataAdapter da = new SqlDataAdapter("select count(bltype) from ajd where bltype=1",cn);
    DataSet ds = new DataSet();
    da.Fill(ds, "ajd");
    this.TextBox1.Text = ds.Tables[0].Rows[0][0].ToString()
      

  20.   

    应该不是数据库连接和语句的问题,是楼主把代码写到了TextBox1_TextChanged事件里的问题,如果你的是网页,那么这个事件根本就不会被激发,改为放到page_load里;如果是应用程序,那么需要在textbox1中随便输入一个东西才会激发这个事件。
      

  21.   


    没看出来  不过adapter 是不用cn.open的 这点可以肯定
      

  22.   


    //select count(bltype) from ajd where bltype=1
    //这个返回单个值  最好别使用SqlDataAdapter  SqlCommand执行效果最好 
      

  23.   

    AutoPostBack="True"这个是蛮重要的还有就是连接打开
      

  24.   

    SqlConnection cn = new SqlConnection("server=localhost;database=jtgl;user=sa;password=sa");
    cn.open();
      SqlCommand cmd = new SqlCommand("select count(bltype) from ajd where bltype=1", cn);
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      DataSet ds = new DataSet();
      da.Fill(ds, "ajd");
    cmd.dispose();
    da.dispose();
    cn.close();  this.TextBox1.Text = ds.Tables[0].Rows[0][0].ToString();  
      

  25.   

    有点意思,看看最后结果是怎样的。估计是网页并没有postback
      

  26.   

    cn.open();
    lz把代码写在点击事件里吧。。
    TextBox1_TextChanged 这个事件谁知道你有没有触发他呀(楼上已经说过了)