I have an SQL command in my code behind as:
cmd = new SqlCommand("insert into MappingInfo(ActivationDate) values (@ActivationDate)", con);
I want to assign 'current timestamp' or 'null' to @ActivationDatebased on some condition, as shown below:
int ActivateState;
if (rdoActivateMappingYes.Checked)
{
ActivateState = 1;
cmd.Parameters.AddWithValue("@ActivationDate", "CURRENT_TIMESTAMP");
}
else
{
ActivateState = 0;
cmd.Parameters.AddWithValue("@ActivationDate", DBNull.Value);
}
However, upon running the web app, I am getting the following error:
{System.Data.SqlClient.SqlException} : {"Conversion failed when converting date and/or time from character string."}
"CURRENT_TIMESTAMP"is astring, not aDateTimeor something. How about using your local time asDateTime.Nowor your UTC time (as a better way of course) asDateTime.UtcNow?