还有一个问题,如果我想让
<asp:ListItem value="0">显示</asp:ListItem>
中的value也由数据库查询出来的数据填充,改怎么写?

解决方案 »

  1.   

    while(dr.Read())
    {
    drolParentSort.Items.Add(dr["sortname"].ToString());
    }
    if(!dr.Read())
    {
    drolParentSort.Items.Add("设为主类");
    }
      

  2.   

    this.drolParentSort.DataSource=dr
    this.drolParentSort.DataTextField="显示";
    this.drolParentSort.DataValueField="值";
    this.drolParentSort.DataBind();
      

  3.   

    to:lxcc(虫子) 
    -----------
    改后的结果为“值1”“值2”“值3”“设为主类”
    最后一个不是所期望的
      

  4.   

    :P,调整一下顺序if(!dr.Read())
    {
    drolParentSort.Items.Add("设为主类");
    }while(dr.Read())
    {
    drolParentSort.Items.Add(dr["sortname"].ToString());
    }
      

  5.   

    public static void OtherBindVT(string stringconnection,string BindTableName,string BindTypeTxt,string BindValue,System.Web.UI.WebControls.DropDownList BindControl )
    {
    SqlConnection myconn=new SqlConnection(stringconnection);
    try
    {
    myconn.Open();
    string DropDownStr1="select * from "+ BindTableName;
    SqlCommand mycmd=new SqlCommand(DropDownStr1,myconn);
    SqlDataReader myReader;
    myReader=mycmd.ExecuteReader();
    BindControl.DataSource=myReader;
    BindControl.DataTextField=BindTypeTxt;
    BindControl.DataValueField=BindValue;
    BindControl.DataBind();
    myReader.Close();
    }
    catch
    {
    throw new Exception("数据帮定错误!请与管理员联系");
    }
    finally
    {
    if(myconn.State==ConnectionState.Open)
    myconn.Close();
    }
    }
      

  6.   

    private void DropDownList1_content()
    {
    string strConn = "server=localhost;uid=sa;pwd=;database=Naihuo";
    myConnection.ConnectionString = strConn;
    string strSQL = "select distinct SYS_ID from SYSTEM_SUBSYSTEM";
    SqlDataAdapter da = new SqlDataAdapter(strSQL,myConnection);
    DataSet ds = new DataSet();
    da.Fill(ds,"id");
    DropDownList1.DataSource = ds.Tables["id"].DefaultView;
    DropDownList1.DataValueField = ds.Tables["id"].Columns[0].ColumnName;
    DropDownList1.DataTextField = ds.Tables["id"].Columns[0].ColumnName;
    DropDownList1.DataBind();
    }
    刚好我刚做过
      

  7.   

    老大!!!
    这里要用while,使用if的意思就是只读一次看看能不能读上,一般是做判断不是做循环的。或者使用上面的方法直接绑定DataTable什么事都没了!
    if(dr.Read())
    {
    drolParentSort.Items.Add(dr["sortname"].ToString());
    }另外:记得关闭DataReader对象!
      

  8.   

    SqlCommand cmd=new SqlCommand("spGetTopParentID",obj.sqlConn);
    cmd.CommandType=CommandType.StoredProcedure;
    SqlDataReader dr;
    dr=cmd.ExecuteReader();
    drolParentSort.DataSource=dr;
    drolParentSort.DataTextField=dr["sortname"].ToString();
    drolParentSort.DataValueField=dr["sortid"].ToString();
    drolParentSort.DataBind();
    dr.Close();------------------------
    System.InvalidOperationException: 在没有任何数据时进行无效的读取尝试。