There is a DataGridView, a data source is connected to it, I make a request to fetch data from a database table.

private void button4_Click(object sender, EventArgs e) { SqlConnection Search_connection = new SqlConnection("....."); SqlCommand Search_command = new SqlCommand(); Search_command.CommandType = CommandType.Text; Search_command.CommandText = "SELECT * from [dbo].[rukovod] where concat(F_R,I_R,O_R) like @str"; Search_command.Parameters.AddWithValue("@str", "%" + textBox4.Text + "%"); Search_command.Connection = Search_connection; Search_connection.Open(); Search_command.ExecuteNonQuery(); Search_connection.Close(); this.rukovodTableAdapter.Fill(this.stroitelnayaFirma_DBDataSet.rukovod); } 

The table is updated, but the data remains the same as it was.
I drove Query request to Managment Studo, everything works there.
How can I make it so that only the result of the query is displayed in the dataGridView?

  • You open a connection, execute the query and immediately close it. And where is the code that reads the result of the query in the stroitelnayaFirma_DBDataSet? - user2455111
  • it shows how to use SqlDataAdapter msdn.microsoft.com/ru-ru/library/… - user2455111
  • Just in case: using transliterated variable names like rukovod is a rather specific coding style that makes reading the code more difficult. Usually so try not to do. ) - Nick Volynkin

2 answers 2

How is your Search_command and rukovodTableAdapter? You are not working correctly with the data adapter .. if there is a SelectCommand in it, then set the value of the parameters there. And do not do ExecuteNonQuery() when you need to get a sample from the database.

Like that

 SqlConnection Search_connection = new SqlConnection("....."); SqlCommand Search_command = new SqlCommand(); Search_command.CommandType = CommandType.Text; Search_command.CommandText = "SELECT * from [dbo].[rukovod] where concat(F_R,I_R,O_R) like @str"; Search_command.Parameters.AddWithValue("@str", $"%{textBox4.Text}%"); Search_command.Connection = Search_connection; Search_connection.Open(); this.rukovodTableAdapter.SelectCommand = Search_command; this.stroitelnayaFirma_DBDataSet.rukovod.Clear(); this.rukovodTableAdapter.Fill(this.stroitelnayaFirma_DBDataSet.rukovod); Search_connection.Close(); 
  • Swears on this.rukovodTableAdapter.SelectCommand = Search_command; - Danil
  • 1) After all swears certain words? Feel free to repeat the curses of your computer. 2) Have you initially configured a SelectCommand in the DataAdapter? Is it different from the question in the text? - 4per

It seems to me that instead of the option this.rukovodTableAdapter.SelectCommand = Search_command; which was offered to you, it is better to do this:

  1. Add to rukovodTableAdapter using VisualStudio for configuring adapters one more select for which it will automatically generate myFill(...) (you will choose the name of the myFill method or some other one during the configuration process);
  2. and then use the this.rukovodTableAdapter.myFill(this.stroitelnayaFirma_DBDataSet.rukovod); construct to fill in the data this.rukovodTableAdapter.myFill(this.stroitelnayaFirma_DBDataSet.rukovod);

This will reduce the chance of making mistakes and getting an Exception - let Microsoft work for you.

  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin