2012-02-02 35 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