Search code examples
c#asp.netsqlsearchsqldatasource

C#: SQL FilterExpression - Missing Operand Exception


I am following the searchable gridview code listed HERE . I am having trouble with using the FilterExpression. I get the exception:

Missing operand after 'Name' operator... When the exception occurs, FilterExpression = " WHERE Name like 'spencer%'"

The exception occurs in the following code:

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

Any thoughts?


Solution

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

    Should be:

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