문제

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.

도움이 되었습니까?

해결책

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;
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top