I'm trying to fix the code of an insert button. It's a button that inserts data into the database.
Here is my code :
private void button2_Click(object sender, EventArgs e)
{
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
sqlCon.Open();
// string requete = "INSERT INTO [RECAP] VALUES ('" + textBox1.Text + "''" + textBox2.Text + "''" + comboBox2.SelectedValue + "''" + comboBox3.SelectedValue + "''" + textBox5.Text + "''" + textBox6.Text + "''" + Global.Global.GolbVar + "''" + DateTime.Now.ToShortDateString() + "');";
string requete = "INSERT INTO dbo.RECAP(code_reseau, tot_dcl, mont_debou, gch_dep, typ_port, mois, annee, emt_dep, utilisateur, date_maj) VALUES ('" + textBox1.Text + "', " + textBox5.Text + "," + textBox6.Text + "," + comboBox2.SelectedValue + "," + comboBox3.SelectedValue + "," +0+ "," +0+ "," +0+ "," + 0 + "," + 0 + ")";
cmd = new SqlCommand(requete, sqlCon);
cmd.ExecuteNonQuery();
MessageBox.Show("Ajouté !");
sqlCon.Close();
}
Every time I try to run this it generates an exception that says
Incorrect syntax near ','
VALUES ('" +should beVALUES ("'" +Console.WriteLine(requete);and make sure your query looks like what you think it should. It's possible that something in one of your textboxes is causing this error. You should also probably include single quotes'around each value :... + "', '" + ...