I have written a code for store the current date & time value. It was working the day before yesterday exactly as I wanted. But today it shows error "Arithmetic overflow error converting expression to data type datetime. The statement has been terminated."
Can any body help me? Here is my code,
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
string q;
q = "insert into tbl_MR(RequirementFor,Category,MRNO,CreatedBy,CreatedDate)values(@rf,@cat,@mr,@cb,@cd)";
SqlCommand cmd = new SqlCommand(q, con);
cmd.Parameters.AddWithValue("@rf", CCddl.SelectedItem.Text);
cmd.Parameters.AddWithValue("@mr", TextBox1.Text.Trim());
cmd.Parameters.AddWithValue("@cat", TextBox3.Text.Trim());
cmd.Parameters.AddWithValue("@cb", Session["loginid"].ToString());
string cd1, date1;
cd1 = System.DateTime.Today.ToShortDateString();
date1 = Convert.ToDateTime(cd1).ToString("dd/MM/yyyy");
cmd.Parameters.AddWithValue("@cd", date1.ToString());
cmd.ExecuteNonQuery();
string alertmessage = "";
alertmessage = "Component Details Saved";
this.CreateMessageAlert(this, alertmessage, "alertKey");
con.Close();
}
You don’t need to convert a date to a string in order to send it to SQL Server.
Instead of all this:
Just do this: