I have the following table in sql server
From C# I have a form that can edit these fields but I get the following exception but I have other tables with the same code and the same structure and it doesn't come out it only comes out with this one why does this happen
System.FormatException: 'String was not recognized as a valid DateTime.'
Why is this happening ?
If you parse the error:
The value returned from
dataGridView1.CurrentRow.Cells[10].Value
does not have a suitable format that can be converted toDateTime
usingConvert.ToDateTime(...)
The string you are trying to convert is "6/19/2019 3:40:00 PM" which would have a format:
Instead of
Convert.ToDateTime(...)
, I suggest you useDateTime.ParseExact(...)
it like this:It is important to comment that the formats must be defined:
M/dd/yyyy h:mm:ss tt
andMM/dd/yyyy hh:mm:ss tt
, since sometimes you can have 2 digits in the month and two digits in the hour