Filter with SQL query from a combobox - c#

Asked

Viewed 161 times

0

I am creating a stock program and I would like the item query to be filtered by a combobox, that is, when writing in the search field only the results related to the combobox parameter 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 typing in the search field the parameter some and all the results are shown, without any filter, someone can help me to solve this problem that afflicts me?

  • you need to qualify the filter and work with its conditions... present the rest of the method if you have no help

  • Post details of the context, because the statement is very vague. For example: several subcategories of a single category OR several pairs category+subcategory can be marked on the combobox?

1 answer

0

Only make a check if the value is filled and if it is null or empty you return to 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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.