在页面上设置了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); }
这是查询的那段代码
根据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); }
这是查询的那段代码
if(t1.text!="")
where+= " and a='"+a+"'"if(t2.text!="")
where+= " and b='"+b+"'"应该这样来处理.
吧 全改了 你 就 好 了
这个条件不行了。
如果这第一个为空他不进来了。他就不执行
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();
}
其实你应该要这样写是最好的。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;然后再邦定。这样是支持多项查询的。
放到查询分析器上执行下不通过就知道哪错了
其他楼的,按照:
string where=" where 1=1";
if(t1.text!="")
where+= " and a='"+a+"'" if(t2.text!="")
where+= " and b='"+b+"'"
不管填写哪个TEXTBOX,都查不出任何信息。
按照:
where+=
能查出信息,但是不管下面的TEXTBOX输入什么,都显示第一个TEXTBOX输入的数据。而且删除功能失效了
if(t1.text!="")
where+= " and a='"+a+"'" if(t2.text!="")
where+= " and b='"+b+"'"
拼SQL