在一个Form中我有两个ListBox,一个ComboBox,其中都填充了很多项
我想通过Sql数据库从三个控件中获取他们的选中的值组成条件,从数据库中选择查询
Sql语句我能写,可是它不能实现我所有要求,原因是,ListBox和ComboBox我都是用SelectedItem属性实现的
那样我只能实现的三个控件都选择了值后,才能执行程序
而我想实现的是他们三个的组合选择,他又很多种情况,但是,我用了SelectedItem属性后,若那个控件不选择值的话,就会提示错误,我应该怎么做,才能到达我想要的那种功能
希望高手指点迷经!
我在线等待,急,先谢为快!!!

解决方案 »

  1.   

    sql:
    string sql=select * from table;
    string sql1=where 1=1;
    string sql2=and 1=1;
    string sql3=and 1=1;根据你的 控件的值得情况 分别对sql1,sql2,sql3 进行赋值。组合最后的SQL string strSql=str+str1+sr2+str3;
      

  2.   

    我会写SQL语句,你的思路与我一样,我说的是我从listbox 中获取值,但有的时候我不想从中选择值的话
    就要提示错误
    出错信息:未将对象应用到实例
    大哥,你把我的意思理解错了
      

  3.   


    那么你每个控件都新增一个初始栏  就设个默认值  让它 CHECKED  就好了 啊 
      

  4.   

    丫,貌似有个 selectedvalue  属性吧  用这该不会出问题了哦
      

  5.   

    SelectedValue应该也会出问题吧,因为系统它要先判断是否Selected了没有,若你用了这个方法,但你又没有选择它的话,一样会提示什么未将对象引用到实例!!!
      

  6.   

    对了,listbox没有SelectedValue方法吧,我刚看了一下
      

  7.   

    5 楼的方法就是 加 一个默认的栏  1=1  可以不?
    怎么个默认的就是你在组合SQL 的时候 自己去取这一栏的值
      

  8.   

    string sql = "select * from Table where 1=1";
                if (textBox1.Selectedvalue != null)
                {
                    sql+=" and 写条件......"
                }
                if(....以此类推只不过是先做判断后拼sql串啊)
      

  9.   

    我知道呀,楼上的大哥们,我不是不能写SQL语句呀,我是说你在条件语句的时候,用了SelectedItem的话
    若你不选择这个ListBox的话,就要提示错误,就必须选择这个ListBox的内容才行,可是我我一定要选择
    最后在此重申
    我不是不会写sql语句,语句我已设计好了,就跟11楼的雷同,不过他的textBox错了
      

  10.   

    看来大家是没有理解我的组合选择
    就是有三个ListView
    我可能选择其中一个或者其中两个,或者三个都选
    我的问题是,我是用这三个ListView的SelectedItem来得到他们的值的,可是当我只选其中一个或者其中两个的时候就要提示错误,我该怎么修改才能正确
      

  11.   

    LZ 你的问题似乎还是不够明确.. 你说3个ListView 取SelectItem  ..你的错误是在取值时发生的..还是取了值后执行SQL 发生的!具体是什么错误? 我建议你贴段代码上来,把错误也贴上 这样大家一看就明白!
      

  12.   

    这是一个按钮的事件
    SqlCommand cmd1 = new SqlCommand();           string[] array1 = new string[5];
               array1[0] = txt_ch_name.Text.ToString ().Trim ();           array1[1]=this .listBox1 .SelectedItem.ToString();
               array1[2]=this .listBox1 .SelectedItem.ToString();
               array1[3] = txt_ch_singer.Text.ToString ().Trim ();           array1[4] = comb_ch_number.SelectedItem.ToString();
               
               string str1 = "";
               str1 = query2.query_list(array1);           cmd1.CommandText = "select * from song_table where " + str1;
              
               cmd1.Connection = strconn.conn;
               SqlDataReader read = cmd1.ExecuteReader();
               string[] array = new string[8];
               this.listView2.Items.Clear();
               while (read.Read())
               {
                   array[0] = read.GetSqlInt32(0).ToString();
                   array[1] = read.GetSqlInt32(1).ToString();
                   array[2] = read.GetSqlString(2).ToString();
                   array[3] = read.GetSqlString(3).ToString();
                   array[4] = read.GetSqlString(4).ToString();
                   array[5] = read.GetSqlString(5).ToString();
                   array[6] = read.GetSqlString(6).ToString();
                   array[7] = read.GetSqlInt32(7).ToString();
                   this.listView2.Items.Add(new ListViewItem(array));
               }
               read.Close();
    下面是一个类,我用一个query的query_list方法来实现返回sql语句的
    public class query1
        {
              public string query_list(string []arr)
              {
                  string str = "1=1";
                  string[] arr1 = new string[5]{"song_name","song_language","song_type","singer","song_wordcount"};
                  for (int i = 0; i < 5; i++)
                  {
                      if (i < 4 && arr[i] != "")
                          str += " and " + arr1[i] + "='" + arr[i] + "'";
                      else
                      {
                          if (arr[i] != "")
                              str += " and " + arr1[i] + "=" + arr[i];
                      }
                  }
                  return str;
              }    }
      

  13.   

    如果我只选其中的ListBox中的项,那另外一个ListBox就要提示说:未将对象引用到实例
    只有全部都选择的话,才会通过调试!!!但那样功能就太单一了
      

  14.   

    加个判断有没有选中,选中就读取ListBox的值,没有就不取呗,SQL文也不加
      

  15.   

    问题应该出在listBox1.SelectedItem.ToString() 如果你的listBox1没选中的话 SelectedItem 是null值  .null值是不能ToString();所以你可以再取之的时候先判断下是否为nullstring listSelectedValue=string.Empty;//默认选中值为空
    if(listBox1.SelectedItem!=null){
     listSelectedValue= this .listBox1 .SelectedItem.ToString(); 
    }
    array1[1]=listSelectedValue;
      

  16.   

    用ArrayList 再循环出来将它赋值就可以了
      

  17.   

    感觉说的有道理。我刚才试过了,虽然不提示什么错误
    但是,只有这几个Listview选择了才会有结果显示,不然就什么都没有