繁体   English   中英

错误关键字“ WHERE”附近的语法不正确,用于过滤Gridview的复选框列表

[英]Error Incorrect syntax near the keyword 'WHERE', checkbox list to filter Gridview

我想用复选框列表项过滤网格视图,但是收到以下错误:关键字“ WHERE”附近的语法不正确。

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem>MT</asp:ListItem>
            <asp:ListItem>AT</asp:ListItem>
        </asp:CheckBoxList>

背后的代码

protected void Button1_Click(object sender, EventArgs e)
        {
           DataTable dt = new DataTable();
            String strConnString = System.Configuration.ConfigurationManager.
                ConnectionStrings["carConnectionString"].ConnectionString;
            string strQuery = "SELECT Namecar, Images,GB,Tip as finalresult FROM Cdetail3 WHERE (Ghe BETWEEN @Start AND @End) order by finalresult DESC";
            string condition = string.Empty;
            foreach (ListItem item in CheckBoxList1.Items)
            {
                condition += item.Selected ? string.Format("'{0}',", item.Value) : string.Empty;
            }

            if (!string.IsNullOrEmpty(condition))
            {
                condition = string.Format(" WHERE GB IN ({0})", condition.Substring(0, condition.Length - 1));
            }

我想这是因为,检查清单在订购后按何处放置,但是如何固定!

谢谢你的帮助

当用户输入以下内容时...

if (!string.IsNullOrEmpty(condition))
{
     condition = string.Format(" WHERE GB IN ({0})", condition.Substring(0, condition.Length - 1));
}

...查询中将有两个'WHERE',这会引发错误。

您可以先将查询的“选择”部分与“哪里”部分分开

string strQuery = "SELECT Namecar, Images,GB,Tip as finalresult FROM Cdetail3"
string condition = "WHERE ..."

将逻辑应用于该条件字符串,然后将strQuery和condition合并在一起

您可以通过以下链接使用“ AND”合并两个WHERE: https : //docs.microsoft.com/zh-cn/sql/t-sql/queries/where-transact-sql

请按照以下步骤从strQuery中删除WHERE:

string strQuery = "SELECT Namecar, Images,GB,Tip as finalresult FROM Cdetail3

并将其置于条件中:

condition = string.Format(" WHERE GB IN({0}) AND (Ghe BETWEEN @Start AND @End) order by finalresult DESC", condition.Substring(0, condition.Length - 1));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM