Hi PRA,
If your dateReceipt column datatype is DateTime then you can save dateReciept.Value in MM/dd/yyyy or MM-dd-yyyy format as database expects these format.
If you are trying to save in dd-MM-yyyy fromat you will get Arithmetic overflow error converting expression to data type datetime.
And as you are inserting record to database you need to use cmd.ExecuteNonQuery() instead of cmd.ExecuteReader().
It is better to use parameterised query instead of inline query.
Check the below example.
private void btnInsert_Click(object sender, EventArgs e)
{
string connectionString = @"Data Source=.;Initial Catalog=Test;User ID=sa;Password=password";
SqlConnection con = new SqlConnection(connectionString);
string query = "INSERT INTO dbo.PersonDate(birthdate) VALUES(@Birthdate);";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@birthdate", dateTimePicker1.Value.ToString("MM-dd-yyyy"));
con.Open();
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("CounterData successfully added", "SAVED - Fronty", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Throwing Exception - Fronty", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
finally
{
con.Close();
}
}