在页面上设置了3个TEXTBOX,用来输入信息;一个GRIDVIEW,用来显示数据;还有一个按钮BUTTON。
根据TEXTBOX里输入的数据,点击BUTTON,在GRIDVIEW里显示相应的内容。现在的问题是,只有第一个TEXTBOX输入数据进行查询好用,其他两个都不好使。
  string where = "";
        if (txtCDM.Text.Trim() != "" || txtVMC.Text.Trim() != ""
            || ddlNYXBZ.SelectedValue != "-1")//查询条件至少有一个不为空
        {
            if (this.txtCDM.Text.ToString() != "")
            {
                if (!IsNumericRegex(this.txtCDM.Text))
                {                    MessageBox.MessageShow("代码类型不正确!", false);                }
                else
                {
                    where = "and CDM='" + txtCDM.Text + "'";
                               if (txtVMC.Text.Trim() != "")
                {
                    where = " and VMC='" + txtVMC.Text + "'";
                }                if (ddlNYXBZ.SelectedValue != "-1")
                {
                    where = " and NYXBZ=" + ddlNYXBZ.SelectedValue;
                }                //根据查询条件列表显示相应的数据
                BB_HJZX_CZYZBXXB bll = new BB_HJZX_CZYZBXXB();
                DataSet ds = bll.GetList(where);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
    }
        else
        {
            MessageBox.MessageShow("条件不能为空!", false);        }
        这是查询的那段代码

解决方案 »

  1.   

    你查询前输出一下where的内容看看,然后拿到查询分析器里面执行一下看看那里错了
      

  2.   

    string where=" where 1=1";
    if(t1.text!="")
     where+= " and a='"+a+"'"if(t2.text!="")
     where+= " and b='"+b+"'"应该这样来处理.
      

  3.   

    汗 where += ("and CDM='" + txtCDM.Text + "'");
    吧  全改了  你 就 好 了 
      

  4.   

      if (this.txtCDM.Text.ToString() != "") 
    这个条件不行了。
    如果这第一个为空他不进来了。他就不执行
    if (txtVMC.Text.Trim() != "") 
                    { 
                        where = " and VMC='" + txtVMC.Text + "'"; 
                    }                 if (ddlNYXBZ.SelectedValue != "-1") 
                    { 
                        where = " and NYXBZ=" + ddlNYXBZ.SelectedValue; 
                    } 
    应该要。if (this.txtCDM.Text.ToString() != "") 
                { 
                    if (!IsNumericRegex(this.txtCDM.Text)) 
                    {                     MessageBox.MessageShow("代码类型不正确!", false);                 } 
                    else 
                    { 
                        where = "and CDM='" + txtCDM.Text + "'"; 
                    } 
                    
                    if (txtVMC.Text.Trim() != "") 
                    { 
                        where = " and VMC='" + txtVMC.Text + "'"; 
                    }                 if (ddlNYXBZ.SelectedValue != "-1") 
                    { 
                        where = " and NYXBZ=" + ddlNYXBZ.SelectedValue; 
                    }                 //根据查询条件列表显示相应的数据 
                      BB_HJZX_CZYZBXXB bll = new BB_HJZX_CZYZBXXB(); 
                    DataSet ds = bll.GetList(where); 
                    GridView1.DataSource = ds; 
                    GridView1.DataBind(); 
                
            } 
      

  5.   

    个人觉得你这样的判断不太好。你这样是不支持同时两个以上输入内容查询。
    其实你应该要这样写是最好的。if (this.txtCDM.Text.ToString() != "") where = " VMC='" + txtVMC.Text + "'"; if (txtVMC.Text.Trim() != "")
    {
      if (where.Trim() != "") where += " and "
      where = " VMC='" + txtVMC.Text + "'"; 
    } if (ddlNYXBZ.SelectedValue != "-1") 
    {
      if (where.Trim() != "") where += " and "
      where = " NYXBZ=" + ddlNYXBZ.SelectedValue; 
    } if (where.Trim() != "") 你的SQL变量 = 你的SQL变量 + " where " + where;然后再邦定。这样是支持多项查询的。
      

  6.   

    最简单的方法把SQL语句输出来
    放到查询分析器上执行下不通过就知道哪错了
      

  7.   

    5楼的,我一开始是像你那样写的,不行。而且还有一个错误是,如果代码类型不正确,点击确定后GRIDVIEW会把所有数据都显示出来。所以ELSE后面的那个括号不能要。
    其他楼的,按照:
    string where=" where 1=1"; 
    if(t1.text!="") 
    where+= " and a='"+a+"'" if(t2.text!="") 
    where+= " and b='"+b+"'" 
    不管填写哪个TEXTBOX,都查不出任何信息。
    按照:
    where+=
    能查出信息,但是不管下面的TEXTBOX输入什么,都显示第一个TEXTBOX输入的数据。而且删除功能失效了
     
      

  8.   

    string where=" where 1=1"; 
    if(t1.text!="") 
    where+= " and a='"+a+"'" if(t2.text!="") 
    where+= " and b='"+b+"'" 
    拼SQL