-1
I am developing an application in C# windows form in 3 layers. My problem is: I have a datagridview
to receive data from the bank and a textbox
to be typed the name I want to search for in the bank, but as I am doing in 3 layers I am not using the textbox
in my sql query that I normally see on the internet like this:
"select * from table Where name like '%" + txt_name +"%'"
I wanted to know how I do this consultation, I tried several ways but without result.
Bank code`
public DataTable Consultar(DadosPessoa nome)
{
try
{
conexao = new MySqlConnection(conecta_db);
MySqlCommand comando = new MySqlCommand("SELECT * FROM funcionario WHERE nome_func like @nome", conexao);
comando.Parameters.AddWithValue("@nome", nome.Nome_Func);
MySqlDataAdapter da = new MySqlDataAdapter(comando);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
catch (Exception erro)
{
throw erro;
}
}`
This is my code of the class that accesses the bank:
public DataTable Consulta_Dao(string nome)
{
try
{
dao = new Classe_Dao();
DataTable dt = new DataTable();
dt = dao.Consultar(nome);
return dt;
}
catch (Exception erro)
{
throw erro;
}
}
This is the button you search for:
private void btn_pesquisar_Click(object sender, EventArgs e, string nome)
{
try
{
ClasseBll bll = new ClasseBll();
nome = txt_pesquisar.Text;
dg_dados.DataSource = bll.Consulta_Dao(nome);
}
catch (Exception erro)
{
throw erro;
}
}
And what’s the problem or mistake?
– Marco Souza
Tell us what it is, and where the error occurs. Because analyzing the code like this on top of it, you don’t seem to have problems. It would only tell you to change the query, not concatenate it, but use the.Parameters.Addwithvalue("name", name);
– Erico Souza
the problem is that when I click the button to search it does not answer me at all.
– Aécio Cleysson
It looks fine. To me, it’s the query that isn’t finding anyone with that name. It shows the code of "Query".
– Renato Afonso
I’ve already edited and entered the bank code
– Aécio Cleysson
@Aéciocleysson Also check if the button has the event Click for the method btn_search_Click or if it is empty. This check can be done in the . design file or depending on the editor, in the events part of the button.
– Henrique