多条件动态查询求助!!!

LookForever 2010-05-15 05:49:01
代码: protected void Page_Load(object sender, EventArgs e)
{
//判断用户是否登录
if (Convert.ToString(Session["username"]) == "")
{
//如果未登录给出提示并返回登录页
Response.Write("<script language=javascript>alert('请登录');location='../login.aspx'</script>");
}
if (!IsPostBack)
{
Session["sql"] = "";
this.bind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
string id = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
Response.Write("<script>window.open('详细信息.aspx?id=" + id + "','','width=450,height=450')</script>");
Response.Write("<script>location='javascript:history.go(-1)'</script>");
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string Sql = " ";
if (CheckBox1.Checked)
{
Sql = Sql + "and CompanyName like '%" + TN.Text + "%' ";
}
if (CheckBox2.Checked)
{
Sql = Sql + "and Type like '%" + PN.Text + "%' ";
}
Session["sql"] = Sql;
this.bind();
}
public void bind()
{
string strsql;
strsql = "select * from wzq_weixiu " + Session["sql"] + " order by id desc";
SqlConnection strcon = new SqlConnection(ConfigurationManager.ConnectionStrings["wzqConnectionString"].ConnectionString);
strcon.Open();
SqlDataAdapter sda = new SqlDataAdapter(strsql, strcon);
DataSet ds = new DataSet();
sda.Fill(ds, "search");
//设置GridView控件数据源
GridView1.DataSource = ds.Tables["search"];
//设置GridView主键
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
Session["sql"] = "";
this.bind();
}
}



strsql = "select * from wzq_weixiu " + Session["sql"] + " order by id desc";这话有问题不知道怎么改,提示 在关键字 'and' 附近有语法错误。 求助大神!!
...全文
112 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
LookForever 2010-05-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bossma 的回复:]
问题所在地:

C# code
if (CheckBox1.Checked)
{
Sql = Sql + "and CompanyName like '%" + TN.Text + "%' ";
}
if (CheckBox2.Checked)
{
Sql = Sql + "and Type like '%" + PN.Text + "%' ";
}……
[/Quote]

问题解决了..是 Select 语句的问题 改成strsql = "select * from wzq_weixiu where id is not null " + Session["sql"] + " order by id desc";
LookForever 2010-05-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
string Sql = "select * from wzq_weixiu where 1=1";
Sql+ = CheckBox1.Checked?"and CompanyName like '%" + TN.Text + "%' ":"";
Sql+ =CheckBox2.Checked? "and Type like '%" + PN.Text + "%' ":"";
单步跟踪查看s……
[/Quote]

错误提示 :
在关键字 'and' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'and' 附近有语法错误。

源错误:


行 76: SqlDataAdapter sda = new SqlDataAdapter(strsql, strcon);
行 77: DataSet ds = new DataSet();
行 78: sda.Fill(ds, "search");
行 79: //设置GridView控件数据源
行 80: GridView1.DataSource = ds.Tables["search"];

wuyq11 2010-05-15
  • 打赏
  • 举报
回复
string Sql = "select * from wzq_weixiu where 1=1";
Sql+ = CheckBox1.Checked?"and CompanyName like '%" + TN.Text + "%' ":"";
Sql+ =CheckBox2.Checked? "and Type like '%" + PN.Text + "%' ":"";
单步跟踪查看sql
萤火架构 2010-05-15
  • 打赏
  • 举报
回复

问题所在地:
if (CheckBox1.Checked)
{
Sql = Sql + "and CompanyName like '%" + TN.Text + "%' ";
}
if (CheckBox2.Checked)
{
Sql = Sql + "and Type like '%" + PN.Text + "%' ";
}


先判断Sql是否为空
Sql = (Sql==""?"CompanyName like '%" + TN.Text + "%'":"and CompanyName like '%" + TN.Text + "%'");

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧