How to autocomplete label according to a data in textbox?

2

What I need is that when a user types a code in a textbox, "appears" the name related to that code in a label.

In the click event of my save button try with this code but do not do it, until the entire saving process finishes. And what I need is for you to show it and then save it.

 //Agregar el nuevo codigo
SqlCommand cmd = new SqlCommand("Insert into tabla1 (...) VALUES(@cod...)", con);
cmd.Parameters.Add("@cod", SqlDbType.VarChar).Value = codigo.Text;
.
.
.
cmd.ExecuteNonQuery();

//aqui busco el nombre relacionado al codigo ingresado
SqlCommand cmd2 = new SqlCommand("Select Nombre from tabla2 where Cod='" + codigo.Text + "'", con);
cmd2.ExecuteNonQuery();
label3.Text = Convert.ToString(cmd2.ExecuteScalar());
Thread.Sleep(10000);

Thanks for reading.

    
asked by gbianchi 09.05.2018 в 06:31
source

1 answer

0

You just have to do the select before the insert , you can run the select and sleep the code and then the insert will be executed.

SqlCommand cmd2 = new SqlCommand("Select Nombre from tabla2 where Cod='" + codigo.Text + "'", con);
cmd2.ExecuteNonQuery();
label3.Text = Convert.ToString(cmd2.ExecuteScalar());
Thread.Sleep(10000);

SqlCommand cmd = new SqlCommand("Insert into tabla1 (...) VALUES(@cod...)", con);
cmd.Parameters.Add("@cod", SqlDbType.VarChar).Value = codigo.Text;
.
.
.
cmd.ExecuteNonQuery();

Although I do not recommend the structure you have, you should think about adding another button at least one small one next to the textbox and that it executes your select query, that way the user controls if he wants to use that code or not.

    
answered by 04.08.2018 в 03:49