日期:2014-05-18  浏览次数:20398 次

DropDownlist的数据绑定问题 请高手指点
我做测试的一个小web项目   数据库用的是sqlserver2005
我的页面上有一个dropdownlist和gridview和一个按纽,一个lable
我的数据库只有一个表,叫customer,里边有两个字段,userID,userName.共有三条记录,对应的是1,jack       2,tom       3,sven
我单独将数据绑定到gridview的时候   ,可以显示所有的数据。
现在想在dropdownlist中列出所有的用户名,并在点按纽的时候对用户选中的用户名进行查询,将结果显示在gridview中,我在绑定数据到dropdownlist后,dropdownlist可以列出所有的用户名,但点按钮的时候搜索的结果只是第一个用户名的数据,只有jack的数据   ,无论我选的是tom还是sven,结果都是jack的数据,请高手指点一下到底是怎么回事,代码如下:

        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                string   connString   =   "SERVER=localhost;UID=sa;database=winner;pwd=123 ";
                SqlConnection   conn   =   new   SqlConnection(connString);
                DataSet   ds   =   new   DataSet();
                SqlDataAdapter   adapter   =   new   SqlDataAdapter();
                SqlCommand   cmd   =   new   SqlCommand();
                cmd.Connection   =   conn;
                cmd.CommandText   =   "SELECT   userID   ,userName   FROM     customer ";
                adapter.SelectCommand   =   cmd;
                adapter.Fill(ds);
               
                DropDownList1.DataSource   =   ds;
                DropDownList1.DataTextField   =   "userName ";
                DropDownList1.DataBind();
                 
        }
        protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                string   connString   =   "SERVER=localhost;UID=sa;database=winner;pwd=123 ";//@ "Database=fuck;Server=OCDEV12\SQLEXPRESS;Integrated   Security=SSPI;uid=sa;pwd=123; ";//
                SqlConnection   conn   =   new   SqlConnection(connString);
               
                DataSet   ds   =   new   DataSet();
                SqlDataAdapter   adapter   =   new   SqlDataAdapter();
                SqlCommand   cmd   =   new   SqlCommand();
                cmd.Parameters.Add(new   SqlParameter( "@userName ",SqlDbType.VarChar));
                cmd.Parameters[ "@userName "].Value   =   this.DropDownList1.SelectedItem.Text;
                cmd.Connection   =   conn;
  &n