在开发当中遇到这样一个古怪问题:
下面是代码:
    public SqlDataReader GetSong()
    {
        SqlConnection mycon = new SqlConnection(myconnetion);
        
        string select = "";
       
        if (Session["Singer_ID"] != null)
        {
            select = @"select * from YSong
                           where YSingerID=" + Session["Singer_ID"].ToString() + " order by Song_date desc";
        }
        else
        {
            Response.Redirect("../NotLogin.aspx");
        }            SqlCommand cmd = new SqlCommand(select, mycon);
            mycon.Open();            SqlDataReader dr = cmd.ExecuteReader();            return dr;             
    }
    protected void BindDropDownList()
    {
        SqlDataReader dr = GetSong();
        if(dr.Read())
        {
                this.DropDownList1.DataSource = dr;
                this.DropDownList1.DataTextField = "YSongname";
                this.DropDownList1.DataValueField = "YSongID";
                this.DropDownList1.DataBind();
        }
        dr.Close();    }
问题是,当调用了BindDropDownList()方法绑定后,无论如何,DropDownList下拉窗口都没有第一个值。而当数据表中仅有一个值时,则是什么都没有。
比如:
表中有
YSongname  YSongID
121          131
1232          1231
则只能看到第二个
若只有第一行的数据时,则什么都不显示
希望各位高手指点一二...

解决方案 »

  1.   


                    this.DropDownList1.DataSource = dr;
                    this.DropDownList1.DataTextField = "YSongname";
                    this.DropDownList1.DataValueField = "YSongID";
                    this.DropDownList1.DataBind();
           直接这样绑定就ok了,不要加那句!!!
      

  2.   

    同一楼,
    SqlDataReader dr = GetSong();                this.DropDownList1.DataSource = dr;
                    this.DropDownList1.DataTextField = "YSongname";
                    this.DropDownList1.DataValueField = "YSongID";
                    this.DropDownList1.DataBind();
            dr.Close();
    好像不习惯使用datareader做数据源的,因为要把datareader绑定到数据源,datareader不能关,而且connection也不能关,绑定过后才能关,根据使用连接的习惯吧,使用过后都及时关闭,这样再绑定,可能忘记关闭吧,我的想法。一般使用dataset,datatable,arraylist等容器做数据源。