I get an exception whenever I try changing value of DateTimePicker in C# application if I don't change it the program runs successfully - any ideas?
Exception :
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Conversion failed when converting date and/or time from character string.
C# code :
private void button8_Click(object sender, EventArgs e)
{
// SqlConnection cnx = new SqlConnection("Data Source=TDI11-PC\\SQLEXPRESS;Initial Catalog=mjhd;Integrated Security=True");
String req = "insert into dommage_materiel values('" + textBox1.Text +"','" + richTextBox2.Text+ "','" + richTextBox1.Text +"','" + textBox3.Text + "','" +dateTimePicker1.Value+"','" + richTextBox3.Text + "','" + textBox2.Text + "','" + textBox4.Text.ToString()+"','" + textBox5.Text.ToString()+"')";
SqlCommand cmd = new SqlCommand(req, cnx);
cnx.Open();
cmd.ExecuteNonQuery();
cnx.Close();
}
I tried this methods nothing worked:
DateTime.Parse(dateTimePicker1.Value.ToString());
Convert.ToDateTime(dateTimePicker1.Value.ToString());
Note : SQL Server column datatype is Date.