I am puzzled why I am unable to insert a converted date value into my table column when the output of it is the same.
Error:
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
Success (hard coded DateTime):
cmd.CommandText = @"INSERT INTO [TestDB].[Cat1].[Table1] (CreatedOn)
VALUES ('2019-08-22 23:59:59.000')";
Failed (converted DateTime):
cmd.CommandText = @"INSERT INTO [TestDB].[Cat1].[Table1] (CreatedOn)
VALUES ('@CreatedOn')";
//value below has the same output as above (2019-08-22 23:59:59.000)
cmd.Parameters.AddWithValue("@CreatedOn", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
//below failed too
cmd.Parameters.AddWithValue("@CreatedOn", DateTime.Now);
Column Type: