C#:SQL FilterExpression - 缺少操作数异常

时间:2012-02-03 01:24:04

标签: c# asp.net sql search sqldatasource

我正在关注列出HERE列出的可搜索网格视图代码。我在使用FilterExpression时遇到问题。我得到了例外:

  

'名称'之后缺少操作数operator ...当异常发生时,   FilterExpression ="在哪里名称如' spencer%'"

以下代码中出现异常:

protected void BindSGVData()
        {
            //hfSearchText has the search string returned from the grid.
            if (hfSearchText.Value != "")
            {
                RidesSQL.FilterExpression = " WHERE " + hfSearchText.Value; //EXCEPTION HERE!
            }
            DataView dv = (DataView)RidesSQL.Select(new DataSourceSelectArguments());
            //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;
            }
        }

有什么想法吗?

1 个答案:

答案 0 :(得分:6)

RidesSQL.FilterExpression = " WHERE " + hfSearchText.Value; //EXCEPTION HERE!

应该是:

RidesSQL.FilterExpression = hfSearchText.Value; // NO EXCEPTION HERE!