Najaf Haider

Najaf Haider

  • NA
  • 61
  • 947

The conversion of a varchar data type to a datetime data

Dec 13 2018 9:44 AM
protected void Button1_Click(object sender, EventArgs e)
{
ObjConn.Open();
SqlCommand ObjCom = new SqlCommand("Insert into Testing(Date) values('" + DateTime.Now.ToString() + "')", ObjConn);
if (ObjCom.ExecuteNonQuery() == 1)
{
Response.Write("<!-- Inject Script Filtered -->");
}
ObjConn.Close();
BindDataTable();
}
 
This is my code i'm just simply insertDatetime.Now in sql database but it gives me error in DB table Date type is datetime. 
It's working in my laptop but when i run this same code in another PC than it gives me  this error.
 
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
Source Error:
Line 25: SqlCommand ObjCom = new SqlCommand("Insert into Testing(Date) values('" + DateTime.Now.ToString() + "')", ObjConn); Line 26: Line 27: if (ObjCom.ExecuteNonQuery() == 1) 

Answers (2)