Error: Incorrect syntax near '14'-Collection of common programming errors
I have to insert values in the table using insert query…the table stored in the database has 3 columns: 1. Date (DateTime) 2. SensorValue (Float) 3. Difference (Float) Now the values for each column comes from datagridview…..here is the button’s code for inserting
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\dbsave.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
if (con.State == ConnectionState.Closed)
{
con.Open();
}
for (Int32 i = 0; i < dataGridView1.Rows.Count-1; i++)
{
String query1 =
"INSERT INTO " + tbName +
" ( Date, SensorValue, Difference) " + "VALUES (" +
dataGridView1.Rows[i].Cells[0].Value + "," +
dataGridView1.Rows[i].Cells[1].Value + "," +
dataGridView1.Rows[i].Cells[2].Value + ")";
SqlCommand cmd1 = new SqlCommand(query1, con);
cmd1.ExecuteNonQuery();
}
con.Close();
MessageBox.Show("The table has been saved");
The error is ofcourse in execution of the query….the first entry in the date column is the value: 12/05/2012 14:32:00….so basically the sql is not accepting the colon that is placed with 14….how can i solve this problem ? please help