Question

I'm trying to search a database in gridview. When I type anything in the textbox I get this error:

syntax error missing operand before 'like' operator

This is the code:

private void txtGrid_TextChanged(object sender, EventArgs e)
{
    DataView dv = new DataView(dt);
    dv.RowFilter = ""+cbGrid.Text + " like '%" + txtGrid.Text + "%'";
    gridPlayers.DataSource = dv;
}

I thought that it might be the ', but I have searched and read all other relevant questions I could find.

Was it helpful?

Solution

It sounds like cbGrid.Text is an empty string. If there is a missing operand, it's because it was not provided (it was blank).

A possible solution is to make sure to only run the filter if the value is not empty, or give it a default value.

private void txtGrid_TextChanged(object sender, EventArgs e)
{
    if (!string.IsNullOrWhiteSpace(cbGrid.Text))    
    {
        // only run when not empty
        DataView dv = new DataView(dt);
        dv.RowFilter = ""+cbGrid.Text + " like '%" + txtGrid.Text + "%'";
        gridPlayers.DataSource = dv;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top