例子:
select distinct typename from XC_WJMasterDevType where  typename in('D-BET格式摄像机')
可以查询typename的某一个值。
现在我用一个DropDownList,把数据绑定在控件上,SQL语句
 string sql="select distinct typename from XC_WJMasterDevType where  typename in('"+this.DropDownList1.SelectedItem.Text.Trim() +"')"
每次只能找一个`~
现在我想用个ListBox控件~添加多个数据 
用一个DropDownList控件,然后用一个btn_add_Click将DropDownList1里的内容添加到ListBox1里(可加多个)。
 string sql="select distinct typename from XC_WJMasterDevType where  typename in('"+this.ListBox1.SelectedItem.Text.Trim()+"')"可是报错了~“未将对象引用设置到对象的实例”,请问一个如果用ListBox控件去实现多个选择功能的话,
代码要怎么实现?

解决方案 »

  1.   

    应该是string sql="select distinct typename from XC_WJMasterDevType where typename in('"+this.DropDownList1.SelectedItem.Text.Trim() +"')"

      

  2.   

    先获取到typename 
    然后
    this.ListBox1.Item.Add(new ListItem(typename ,typename ));
      

  3.   


    string sql="select distinct typename from XC_WJMasterDevType where typename in('"+this.ListBox1.SelectedItem.Text.Trim()+"')"listBox1多选的话 this.ListBox1.SelectedItem.Text.Trim()这个只能读取第一个
    改成如下的string str = "";
                for (int i = 0; i < ListBox1.Items.Count; i++)//可以多选
                {
                    if (ListBox1.Items[i].Selected)
                    {
                        str+=ListBox1.Items[i].Text+",";
                    }
                }
                str = str.Substring(0,str.Length-1);
    string sql="select distinct typename from XC_WJMasterDevType where typename in('"+str+"')"
      

  4.   

    循环选定的项,把text拼起来传入sql
      

  5.   

    string str = "";
                for (int i = 0; i < ListBox1.Items.Count; i++)//可以多选
                {
                    if (ListBox1.Items[i].Selected)
                    {
                        str+=ListBox1.Items[i].Text+"‘,’";
                    }
                }
                str = str.Substring(0,str.Length-1);
    string sql="select distinct typename from XC_WJMasterDevType where typename in('"+str+"')"
    这样就对了~唯一恶心地方就是加到LISTBOX中后,非要鼠标点击一个才算选取中?要按CTRL键才能多选取?
    有没有办法BTN_ADD后就默认为选中啊?
      

  6.   

    就是说`我加到LISTBOX1中的值~非要用鼠标点击才能加到string sql="select distinct typename from XC_WJMasterDevType where typename in('"+str+"')"
    的where typename in('"+str+"')"中`~两三个记录的话非得按下CTRL进行多选取才能`选取中值~
    我后来用这个:
            string str="";
    if(ListBox1.Items.Count>0)
    {
    foreach(ListItem item in ListBox1.Items)
    {
     this.sql+="select distinct typename from XC_WJMasterDevType where typename in('"+ str.Trim() +"','"+ item.Value +"','"+ item.Text +"')";
    }
    现在不用点击了`ADD进LISTBOX1中就可以了`
    但是出现一个问题`比如ADD进`~两个记录 A和B~但上面的代码`~会这样走~(‘A’,‘B’,‘A’,‘B’)
    怎么会这样啊`我要的结果是~(‘A’,‘B’)`
    就是这个意思`~你的代码`是ADD记录进来了`~~但是生成报表时非要按下CTRL进行多选取才能`选取中值生成报表`而且是不点不行`不按不行`~