I am using a date out of a SQL Server database that has the miliseconds on it like the example here "2016-10-07 21:00:29:987" which is held in a hidden field.
This is how the data is recorded, and this is one of the clauses of an update in a database.
When I try and pass this into the database as a parameter I get the "String was not recognized as a valid DateTime" error when using
var d = DateTime.ParseExact(hfDateLocked.Value.ToString(), "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);
If I remove the milliseconds off this works fine, but as its in the database with the milliseconds and this is one of the three clauses \ conditions I need to pass it through with this.
Any help would be gratefully received.
Edit 1
SqlParameter lockTimParam = new SqlParameter("@LockTime", SqlDbType.DateTime);
if (string.IsNullOrEmpty(hfLockTime.Value.ToString()))
{
lockTimParam.Value = DBNull.Value;
}
else
{
lockTimParam.Value = DateTime.ParseExact(hfLockTime.Value.ToString(), "yyyy-MM-dd hh:mm:ss:fff", CultureInfo.InvariantCulture);
}
DateTimeobjecthfDateLockedand you're usingDateTime.Parse? This doesn't make any sense. If you want to format your DateTime object to a specific string format, you can just call.ToStringand pass the desired format. Check the answer below.