ASP.NET可搜索的GridView异常

时间:2012-02-02 00:05:29

标签: c# asp.net sql gridview where-clause

我正在关注THIS解决方案以使gridview可搜索。它似乎非常接近工作,但我无法弄清楚:下面:我尝试搜索时的异常。任何想法都将不胜感激。

protected void BindSGVData()
        {
            //hfSearchText has the search string returned from the grid.
            if (hfSearchText.Value != "")
                RidesSQL.SelectCommand += " where " + hfSearchText.Value;
            DataView dv = (DataView)RidesSQL.Select(new DataSourceSelectArguments()); //EXCEPTION HERE!!!
            //hfSort has the sort string returned from the grid.
            if (hfSort.Value != "")
                dv.Sort = hfSort.Value;

            RideSGV.DataSource = dv;
            try
            {
                RideSGV.DataBind();
            }
            catch (Exception exp)
            {
                //If databinding threw exception bcoz current page index is > than available page index
                RideSGV.PageIndex = 0;
                RideSGV.DataBind();
            }
            finally
            {
                //Select the first row returned
                if (RideSGV.Rows.Count > 0)
                    RideSGV.SelectedIndex = 0;
            }
        }

例外:

关键字'其中'。

附近的语法不正确

- > hfSearchText.Value包含:"名称类似于' Spencer%'"

1 个答案:

答案 0 :(得分:1)

我猜你的orignal select语句中有一个表达式。这意味着您的代码不起作用,因为您将where子句附加在错误的位置,您需要从原始select命令中删除顺序,并在SQL之外进行排序。或者我认为您可以使用DataSources FilterExpression属性。如果你替换

RidesSQL.SelectCommand += " where " + hfSearchText.Value;

RidesSQL.FilterExpression = hfSearchText.Value;

我认为你会避免语法错误。