Filter with SQL query from a combobox - c #

0

I am creating a stock program and would like the query of items to be filtered by a combobox, ie, when typing in the search field only the results regarding the parameter of the combobox appear.

This is my SQL query code:

DataTable tabela = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("Select " +
                                       "sc.scat_cod, sc.scat_nome, sc.cat_cod, c.cat_nome " +
                                       "from subcategoria sc " +
                                       "inner join categoria c on sc.cat_cod = c.cat_cod " +
                                       "where " +
                                       "(sc.scat_nome like '%" + valor + "%') or (c.cat_nome like '%" + valor + "%')", conexao.StringConexao);

da.Fill(tabela);
return tabela;

With this code when entering in the search field the parameter add and all results are shown, without any filter, can someone help me solve this problem that distresses me?

    
asked by anonymous 12.10.2018 / 21:54

1 answer

0

Just do a check if the value is filled and if it is null or empty you return the DataTable with nothing.

DataTable tabela = new DataTable();

If(String.IsNullOrEmpty(valor)) return tabela;

SqlDataAdapter da = new SqlDataAdapter("Select " +
                                       "sc.scat_cod, sc.scat_nome, sc.cat_cod, c.cat_nome " +
                                       "from subcategoria sc " +
                                       "inner join categoria c on sc.cat_cod = c.cat_cod " +
                                       "where " +
                                       "(sc.scat_nome like '%" + valor + "%') or (c.cat_nome like '%" + valor + "%')", conexao.StringConexao);

da.Fill(tabela);
return tabela;
    
15.10.2018 / 11:57