Reputation: 1
This is the error. sam code parse same value in my other project, but not working in current project Exception Type "System.FormatException" occurred in mscorlib.dll Additional info: string was not recognize as valide Date time
DateTime temp = DateTime.ParseExact(dataGridView1.Rows[i].Cells["Date/Time"].Value.ToString(), "dd/MM/yyyy HH:mm", null);
DateTime tempnext = DateTime.ParseExact(dataGridView1.Rows[i + 1].Cells["Date/Time"].Value.ToString(), "dd/MM/yyyy HH:mm", null);
Upvotes: 0
Views: 547
Reputation: 18310
You're using 'DateTime.ParseExact()' with the format dd/MM/yyyy HH:mm
, but your input is 13:1
which would only be the format HH:m
or H:m
.
Because you're using ParseExact
the input must be the in the exact same format as you've specified, so you would need an input like 05/03/2017 13:01
for it to work.
Consider using the regular DateTime.Parse()
or, as already suggested, DateTime.TryParseExact()
in order to verify the input without breaking the application.
Upvotes: 1
Reputation: 1402
If it works in one project but not another, it could be a Culture
issue.
You can try:
DateTime dateTime = DateTime.ParseExact(dataGridView1.Rows[i].Cells["Date/Time"].Value.ToString(), "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
Although you might want to consider TryParse()
or TryParseExact()
, in the case that this is just 1 invalid value that someone entered, so that the entire program doesn't break down.
Upvotes: 1