2012-02-02 66 views
0

我正在关注THIS解决方案,以使gridview可搜索。这似乎与工作非常接近,但当我尝试搜索时,我无法弄清楚:below:exception。任何想法将不胜感激。ASP.NET可搜索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载:“名称,如‘斯宾塞%’”

+0

查询的条件是什么?这似乎是这样。 “选择价值”。它必须是这样的。 “Select [fields] where [field] .ColumnName = value” – fiberOptics 2012-02-02 01:37:19

+0

看起来代码生成的查询看起来像:“SELECT * FROM [Rides] ORDER BY [TimeOfCall],[Status]其中名称如'Spencer%' “......但我似乎无法弄清楚究竟要改变什么。 – SHeinema 2012-02-02 04:47:35

回答

1

我猜你的一部开拓创新的select语句必须通过它表达的顺序。意思是你的代码不起作用,因为你在错误的地方附加了where子句,你需要从原始select命令中删除命令,然后在SQL之外进行排序。或者我认为你可以使用DataSources FilterExpression属性。如果您有

RidesSQL.FilterExpression = hfSearchText.Value; 

更换

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

我想你会避免语法错误。

+0

谢谢。我现在非常接近,但我仍然得到了一个例外:'Name'操作符后缺少操作数。 FilterExpression =“WHERE名称,如'spencer%'”。对此有何想法? – SHeinema 2012-02-03 01:18:32

+0

nm ...不需要在哪里!谢谢! – SHeinema 2012-02-03 01:32:23

+0

我已编辑答案以反映您的反馈。 – GarethD 2012-02-03 08:44:31