【问题标题】:ASP.NET Searchable GridView ExceptionASP.NET 可搜索的 GridView 异常
【发布时间】:2012-02-02 00:05:29
【问题描述】:

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

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;
            }
        }

例外:

关键字“where”附近的语法不正确。

-> hfSearchText.Value 包含:“Name like 'Spencer%'”

【问题讨论】:

  • 查询的条件是什么?看起来是这样的。 “选择价值”。它必须是这样的。 "选择 [fields] where [field].ColumnName = value"
  • 看起来代码正在生成如下查询:“SELECT * FROM [Rides] ORDER BY [TimeOfCall], [Status] where Name like 'Spencer%'”...但我可以似乎不知道要改变什么。

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


【解决方案1】:

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

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

RidesSQL.FilterExpression = hfSearchText.Value;

我想你会避免语法错误。

【讨论】:

  • 谢谢。我现在非常接近,但我仍然遇到异常:“名称”运算符后缺少操作数。 FilterExpression = " WHERE Name like 'spencer%'" 。对此有什么想法吗?
  • nm...不再需要 WHERE 了!谢谢!
猜你喜欢
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多