I am following THIS solution for making a gridview searchable. It seems to be very close to working but I cannot figure out the :below: exception when I try to search. Any thoughts would be greatly appreciated.
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;
}
}
EXCEPTION:
Incorrect syntax near the keyword ‘where’.
-> hfSearchText.Value contains: “Name like ‘Spencer%'”
I’d guess your orignal select statement have an order by expression in it. Meaning your code doesn’t work because you are appending the where clause in the wrong place, you would either need to remove the order by from the original select command, and do the sorting outside of SQL. Or I think you can use the DataSources FilterExpression property. If you replace
with
I think you will avoid the syntax error.