Why not save data from a dataGridView to a database (SQL Server)? Code without errors.

Code:

using (SqlConnection connection = new SqlConnection(ConnStr)) { connection.Open(); adapter = new SqlDataAdapter(sql, connection); commandBuilder = new SqlCommandBuilder(adapter); adapter.InsertCommand = new SqlCommand("sp_CreateUser", connection); adapter.InsertCommand.CommandType = CommandType.StoredProcedure; adapter.InsertCommand.Parameters.Add(new SqlParameter("@Ρ‚ΠΈΠΏ ΠΏΠΏΠΏ", SqlDbType.Text, 50, "Ρ‚ΠΈΠΏ ΠΏΠΏΠΏ")); adapter.InsertCommand.Parameters.Add(new SqlParameter("@Ρ‚ΠΈΠΏ Ρ„Ρ„Ρ„", SqlDbType.Text, 0, "Ρ‚ΠΈΠΏ Ρ„Ρ„Ρ„")); adapter.InsertCommand.Parameters.Add(new SqlParameter("@Ρ‚ΠΈΠΏ Π·Π·Π·", SqlDbType.Text, 50, "Ρ‚ΠΈΠΏ Π·Π·Π·")); adapter.InsertCommand.Parameters.Add(new SqlParameter("@Ρ‚ΠΈΠΏ ΠΌΠΌΠΌ", SqlDbType.Text, 0, "Ρ‚ΠΈΠΏ ΠΌΠΌΠΌ")); adapter.InsertCommand.Parameters.Add(new SqlParameter("@Ρ‚ΠΈΠΏ ΠΊΠΊΠΊ", SqlDbType.Text, 50, "Ρ‚ΠΈΠΏ ΠΊΠΊΠΊ")); adapter.InsertCommand.Parameters.Add(new SqlParameter("@Ρ‚ΠΈΠΏ Ρ‰Ρ‰Ρ‰", SqlDbType.Text, 0, "Ρ‚ΠΈΠΏ Ρ‰Ρ‰Ρ‰")); SqlParameter parameter = adapter.InsertCommand.Parameters.Add("@β„– азс", SqlDbType.NChar, 10, "β„– азс"); parameter.Direction = ParameterDirection.Output; } 

Closed due to the fact that off-topic participants tym32167 , PashaPash ♦ 28 May '18 at 18:34 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • β€œQuestions asking for help with debugging (β€œ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - PashaPash
If the question can be reformulated according to the rules set out in the certificate , edit it .

1 answer 1

After using(){ ... } connection is closed and unusable.

 SqlConnection connection = new SqlConnection(ConnStr) adapter = new SqlDataAdapter(sql, connection); 

The adapter itself will open and close the connection.

  • thank! you that help - user298557