我将含有参数的查询语句结果绑定到DropDrowList中,代码如下:
  public void getUnitName()
    {
         string un =Session["userName"].ToString();
         string connStr = "server=.;database=tlxt;uid=sa;pwd=;integrated security=SSPI;persist security info=False";
         SqlConnection sqlConn = new SqlConnection(connStr);
         string queryStr = "select ItemInf_Name from xt_ItemInf where Item_Username='" + un + "'";
         DataSet ds = new DataSet();
         SqlDataAdapter  adapter =new SqlDataAdapter (queryStr ,connStr );
         adapter.Fill(ds, "xt_ItemInf");
         DropDownList1.DataSource = ds;   
         DropDownList1.DataBind();       }
但是绑定后的结果却不对,DropDrowList中绑定单元的名称变成了System.Data.DataRowView,而不是查询到的数据,还请大家帮帮忙。

解决方案 »

  1.   

    this.DropDownList1.DataSource = ds; 
    this.DropDownList1.DataTextField = "显示文本";// ItemInf_Name ?
    this.DropDownList1.DataValueField = "隐藏值"; 
    this.DropDownList1.DataBind();
      

  2.   

    this.DropDownList1.DataTextField = "显示文本";// ItemInf_Name ?
    this.DropDownList1.DataValueField = "隐藏值"; 
    这两句什么意思啊,什么是显示文本,什么又是隐藏值?
      

  3.   

    添加两行:DropDownList1.DataTextField = "temInf_Name";
              DropDownList1.DataValueField = "temInf_Name";    DataTextField和DataValueField可以绑定不同的数据表字段。
    完整代码如下:
      public void getUnitName() 
        { 
            string un =Session["userName"].ToString(); 
            string connStr = "server=.;database=tlxt;uid=sa;pwd=;integrated security=SSPI;persist security info=False"; 
            SqlConnection sqlConn = new SqlConnection(connStr); 
            string queryStr = "select ItemInf_Name from xt_ItemInf where Item_Username='" + un + "'"; 
            DataSet ds = new DataSet(); 
            SqlDataAdapter  adapter =new SqlDataAdapter (queryStr ,connStr ); 
            adapter.Fill(ds, "xt_ItemInf"); 
            DropDownList1.DataSource = ds;         DropDownList1.DataTextField = "temInf_Name";
            DropDownList1.DataValueField = "temInf_Name";        DropDownList1.DataBind();      } 
      

  4.   

    你这个函数我看不出在哪里打开了数据,也没看到在哪里关了数据源.最好采用TRY{}CATCH(){}FINALLY{}结构来获取异常信息.你的数据源可以在Webconfig文件中进行配置会更好管理更省时间,编写程序时要符合规范,不然别人很难看懂.你可以这样试一下:
     public void getUnitName() 
        { 
            string un =Session["userName"].ToString(); 
            string connStr = "server=.;database=tlxt;Trusted_connection=true"; //信任连接
            SqlConnection sqlConn = new SqlConnection(connStr); 
            string queryStr = "select ItemInf_Name from xt_ItemInf where Item_Username='" + un + "'"; 
            TRY{
            sqlconn.open();//打开数据源
            DataSet ds = new DataSet(); 
            SqlDataAdapter  adapter =new SqlDataAdapter(queryStr,connStr); 
            adapter.Fill(ds); 
            sqlConn.close();//关闭数据源
            this.DropDownList1.DataSource = ds; 
           this.DropDownList1.DataTextField = "设置显示字段";//要在下拉列表中显示的文字
     
           this.DropDownList1.DataValueField = "设置值字段"; //也就是Value的值一般是这个表的ID因为一般不用它        this.DropDownList1.DataBind();//绑定数据源
    }
          CATCH(Sqlexception ee){
             
               Response.write("<script>alert('数据库出错,错误为:'"+ee.message.ToString()+"'');</script>" );
              
       

    finally{
    sqlConn.close();
    }     } 
      

  5.   

    DropDownList1.DataSource = ds; 
    DropDownList1.DataTextField="ItemInf_Name" ;//这个是在DropDownList显示的文本
    DropDownList1.DataBind();
    如果没有设置DataValueField的话,DropDownList的SelectedValue与显示文本相同
      

  6.   

    public void getUnitName() 
        { 
            string un =Session["userName"].ToString(); 
            string connStr = "server=.;database=tlxt;uid=sa;pwd=;integrated security=SSPI;persist security info=False"; 
            SqlConnection sqlConn = new SqlConnection(connStr); 
            string queryStr = "select ItemInf_Name from xt_ItemInf where Item_Username='" + un + "'"; 
            DataSet ds = new DataSet(); 
            SqlDataAdapter  adapter =new SqlDataAdapter (queryStr ,connStr ); 
            adapter.Fill(ds, "xt_ItemInf"); 
            DropDownList1.DataTextField = "ItemInf_Name";
            DropDownList1.DataValueField = "ItemInf_Name"; 
            DropDownList1.DataSource = ds;
            DropDownList1.DataBind();      } 
      

  7.   

    建议还是加上Try{}cartch(){}进行调试一下
      

  8.   

    this.DropDownList1.DataTextField = "显示文本";
    this.DropDownList1.DataValueField = "隐藏值"; 
    没有这两句话,你就没有指定你要绑定到哪个字段
    如果你的表里面有10个字段,你不写这两句话,你想你的DROPDOWNLIST绑定哪两个字段呀
      

  9.   

    这问题居然有不同的回答......ddl的绑定方式不对,
    想用ddl显示数据源,就需要指定DataTextField,DataValueField 必须指定
    你这样绑定
      DropDownList1.DataSource = ds;  
            DropDownList1.DataBind();  请问,ddl下拉的时候显示什么内容?显示的内容又是什么值?这种基础真是......无话可说
      

  10.   

    this.DropDownList1.DataTextField = "显示文本";// ItemInf_Name ? 
    this.DropDownList1.DataValueField = "隐藏值"; 
    这两句什么意思啊,什么是显示文本,什么又是隐藏值?-------
    DropDownList显示文本就是选择的时候显示的一排文本,隐藏值就是对应文本的值
      

  11.   


    this.DropDownList1.DataSource = ds; //赋数据源
    this.DropDownList1.DataTextField = "字段名称";// 你需要显示的字段 
    this.DropDownList1.DataValueField = "隐藏值";  //这个就相当于绑定下的Value值,当你选择某项时,
                                                           可以取得该选择项的value值this.DropDownList1.SelectedValue
    this.DropDownList1.DataBind();//呈现数据