我要把查询到的数据显示到datagrid的第一行怎么做 大侠帮帮忙吧
private void chaxun_Click(object sender, System.EventArgs e)
{
string strCon="server=XCH;database=aaa;uid=sa;pwd=;"; 
            if(TextBox1.Text.Length>0)  
            {  
string strqry="select admin from Table1 where (admin='"+TextBox1.Text+"')"; 
SqlConnection conn=new SqlConnection(); 
conn.ConnectionString = strCon; 
SqlCommand cmd = conn.CreateCommand(); 
cmd.CommandText = strqry;
cmd.CommandType = CommandType.Text;
conn.Open(); 
cmd.ExecuteScalar(); 
conn.Close(); 
              }  
else   
{   
Response.Write("<script   language=javascript>alert('没有符合条件的表单,请重新查询!');</script>");              
}  
}       

解决方案 »

  1.   

    查询到一条数据,在绑定一下datagrid不就可以了吗?
      

  2.   

    不用asp.net1.1了,它跟asp.net2.0机制有些不同,它需要你自己编写代码维护的东西比较多。
      

  3.   

    string strqry="select admin from Table1 where (admin='"+TextBox1.Text+"')"; 
                    SqlConnection conn=new SqlConnection(); 
                    conn.ConnectionString = strCon; 
                    SqlCommand cmd = conn.CreateCommand(); 
                    cmd.CommandText = strqry;
                    cmd.CommandType = CommandType.Text;
                    conn.Open(); 
                    cmd.ExecuteScalar(); //这个不对
    用一个返回数据集的方法 数据集 = cmd.***       //dr = cmd.excutenoquery()            
    datagrid1.datasource = dr;
    datagrid1.databind();
      

  4.   

    private void databind()
            {
                string sql = "select id,admin,pwd from Table1";
                DataSet ds = new DataSet();
                SqlConnection conn = new SqlConnection("server=XCH;database=aaa;uid=sa;pwd=");
                SqlCommand comm = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter(comm);
                conn.Open(); 
                
                da.Fill(ds,"admin");
                this.Grid1.DataKeyField = "id";
                this.Grid1.DataSource = ds.Tables["admin"].DefaultView;
                this.Grid1.DataBind();
                conn.Close();         
          
            }
            private void Button1_Click(object sender, System.EventArgs e)
            {
                string strCon="server=XCH;database=aaa;uid=sa;pwd=;"; 
                
                if(TextBox1.Text.Length>0)  
                {  
                    
                    string strqry="select admin from Table1 where (admin='"+TextBox1.Text+"')"; 
                    SqlConnection conn=new SqlConnection(); 
                    conn.ConnectionString = strCon; 
                    SqlCommand cmd = conn.CreateCommand(); 
                    cmd.CommandText = strqry;
                    cmd.CommandType = CommandType.Text; 
                    conn.Open();
                    this.Grid1.DataSource = cmd.ExecuteScalar();   
                    this.Grid1.DataBind();
                    conn.Close();
                }  
    这样出来的是一列啊 我要查询一行 
    我是要在admin这列中的一个数据,在表中查询admin这一行的数据
      

  5.   

    修改你的SQL为:string strqry="select top 1 admin from Table1 where (admin='"+TextBox1.Text+"')";