Help solve the problem with the error save to the database.

Error code:

System.Data.SqlClient.SqlException was unhandled by user code HResult = -2146232060 Message = Incorrect syntax near 'work'. Must declare the scalar variable "@ Name".

Code snippet:

if (CheckBox1.Checked) { SqlConnection connection = new SqlConnection(); connection.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename='F:\2016\ITVDN\16 [ITVDN] ASP.NET Базовый\[ITVDN] ASP.NET Базовый\006_DataBinding\002_SqlDataBindingSamples\Survey\App_Data\Database.mdf';Integrated Security=True"; connection.Open(); string sql = @"Insert Into Survey (N'Имя', N'Фамилия', N'Отчество', N'Название конкурсной работы', N'Номинация конкурсной работы',N'Дисциплина', N'Апробация анкеты', N'Уровень', N'Полное название организации', N'Должность', N'Населенный пункт', N'Область', N'Мобильный телефон', N'Как узнали о нас' ) Values(@Имя, @Фамилия, @Отчество, @Название конкурсной работы, @Номинация конкурсной работы, @Дисциплина, @Апробация анкеты, @Уровень, @Полное название организации, @Должность, @Населенный пункт, @Область, @Мобильный телефон, @Как узнали о нас)"; using (SqlCommand cmd = new SqlCommand(sql, connection)) { // Добавить параметры cmd.Parameters.AddWithValue("@Имя", TextBox2.Text); cmd.Parameters.AddWithValue("@Фамилия", TextBox1.Text); cmd.Parameters.AddWithValue("@Отчество", TextBox3.Text); cmd.Parameters.AddWithValue("@Название конкурсной работы", TextBox4.Text); cmd.Parameters.AddWithValue("@Номинация конкурсной работы", DropDownList1.SelectedItem.Text); cmd.Parameters.AddWithValue("@Дисциплина", TextBox5.Text); cmd.Parameters.AddWithValue("@Апробация анкеты", RadioButtonList1.SelectedItem.Text); cmd.Parameters.AddWithValue("@Уровень", TextBox6.Text); cmd.Parameters.AddWithValue("@Полное название организации", TextBox7.Text); cmd.Parameters.AddWithValue("@Должность", TextBox8.Text); cmd.Parameters.AddWithValue("@Населенный пункт", TextBox9.Text); cmd.Parameters.AddWithValue("@Область", DropDownList2.SelectedItem.Text); cmd.Parameters.AddWithValue("@Мобильный телефон", TextBox10.Text); if (String.IsNullOrEmpty(TextBox11.Text)) { cmd.Parameters.AddWithValue("@Как узнали о нас", DropDownList3.SelectedItem.Text.ToString()); } else cmd.Parameters.AddWithValue("@Как узнали о нас", TextBox11.Text); cmd.ExecuteNonQuery(); } connection.Close(); } else Response.Write("Ошибка"); 
  • Try to replace the spaces in the parameter names with _ - 4per
  • @ 3per is now an error Invalid column name 'Name'. Invalid column name 'LastName'.and so with all fields - Nameless
  • Try before the space to put \ - Komdosh
  • one
    Place the field names in square brackets [] in the Insert statement. [Name], [Surname], etc. And a little off topic: is it really convenient to work with such names of controls, do not get confused? - Sleepy Panda
  • Thanks, it helped. I know that it is necessary to change the names of the controllers, but in this case I am not confused. - Nameless

0