问题:
我的界面上有两DropDownList控件,它们的数据源是一样的,都是来自于同一个SqlDataReader,但为什么只有其中一个有数据,另一个没有? 如下代码:SqlConnection myConnection = new SqlConnection(...);
String cmdText  ="SELECT ....";
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
myConnection.Open();
SqlDataReader recm = myCommand.ExecuteReader();
DropDownList1.DataSource = recm;
DropDownList1.DataTextField = "EmployeName";
DropDownList1.DataValueField="EmployeId";
DropDownList1.DataBind();
DropDownList2.DataSource = recm;
DropDownList2.DataTextField = "EmployeName";
DropDownList2.DataValueField="EmployeId";
DropDownList2.DataBind();我知道这种情况用SqlDataReader可能不合适,但我不知该用什么,大家有更好的建议吗?能不能写完整点的代码,因为我很菜的.

解决方案 »

  1.   

    String cmdText  ="SELECT ....";
    DataSet ds=GetDataSet(cmdText);DropDownList1.DataSource = ds;
    DropDownList1.DataTextField = "EmployeName";
    DropDownList1.DataValueField="EmployeId";
    DropDownList1.DataBind();
    DropDownList2.DataSource = ds;DropDownList2.DataTextField = "EmployeName";
    DropDownList2.DataValueField="EmployeId";
    DropDownList2.DataBind();//
    public static DataSet GetDataSet(string sql)
    {
         string ConnectionString="连接字符串";
                SqlDataAdapter    sda =new SqlDataAdapter(sql,ConnectionString);
                DataSet ds=new DataSet();
                sda.Fill(ds);
                return ds;
    }
      

  2.   

    我实例中的完整代码如下:   SqlConnection myConnection = new SqlConnection(GlobalDB.DataBaseDB.ConnectionString);
       String cmdText = "SELECT DISTINCT RoomID FROM Room WHERE RoomBedMan is NULL OR RoomBedMan = 0 ORDER BY RoomID";
       myConnection.Open();
       SqlDataAdapter sdaRoom = new SqlDataAdapter(cmdText,myConnection);
       DataSet dsRoom = new DataSet();
       sdaRoom.Fill(dsRoom);
       addBedRoomID.DataSource = dsRoom;
       addBedRoomID.DataValueField = "RoomID";
       addBedRoomID.DataTextField = "RoomID";
       addBedRoomID.DataBind();
       ddlVacantRoom.DataSource = dsRoom;
       ddlVacantRoom.DataValueField = "RoomID";
       ddlVacantRoom.DataTextField = "RoomID";
       ddlVacantRoom.DataBind();
      }