2

I can now filter on one thing (input is a textbox). But I want to filter on two textboxes. How to do this? I'm using a DataSet, I added this query to it: select * from question where questioncat = @questioncat. In the FillBy i'm using this query. I also want to filter on question (where question = @question)

Code:

private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (txtBxSearch.Text == "") 
            {
                hR5DataSetVraagTableAdapter.Fill(hR5DataSet.question);
            }
            else
            {
                hR5DataSetVraagTableAdapter.FillBy(hR5DataSet.question, txtBxSearch.Text); 
            }
        }

1 Answers1

1

if you want to dynamic set your filter condition you can try to use condition with a parameter to make it.

Dynamically build the filter statement for the SQL query in code skipping any parameters that are empty

private void btnSearch_Click(object sender, RoutedEventArgs e)
{

    var sqlStr = new System.Text.StringBuilder();
    sqlStr.AppendLine("select * from question where 1 = 1 ");

    if(!string.IsNullOrEmpty(txtBxSearch.Text)){
        sqlStr.AppendLine(" AND questioncat = @questioncat ");
         Parameters.Add(new SqlParameter("@questioncat", txtCondition1.Text));
        //and add parameter for your command.
    }

    if(!string.IsNullOrEmpty(txtBxSearch1.Text)){
        sqlStr.AppendLine(" AND questioncat1 = @questioncat1 ");
        Parameters.Add(new SqlParameter("@questioncat1", txtCondition2.Text));
        //and add parameter for your command.
    }
    //.... do your SQL execute.

}

NOTE

WHERE 1 = 1 let you can set your condition with AND operation.

D-Shih
  • 44,943
  • 6
  • 31
  • 51