Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: String was not recognized as a valid DateTime.


//The error line was this, I use the convert method. Please help//
DateTimePicker1.Text = DataGridView1.CurrentRow.Cells(5).Value


What I have tried:

I am a beginner, I don't know what to do
Posted
Updated 13-Dec-21 20:44pm

It's telling you what the problem is. The string of text you're passing into the DateTimePicker control cannot be converted to a DateTime value.

We can't see the string, and you didn't post it, so...

You have to use the debugger to look at the value of the Cell to see what it contains.
 
Share this answer
 
The problem probably goes deeper than you think: you are getting the value from a DataGridView cell and trying to set it as a DateTime value.

That means that the problem isn;t specifically with that code, but with the data you are processing, and the code which read and writes it from whatever storage you use.

At a guess, you are reading this from a DB into a DataTable and using that as the DataSource for the DGV. And that would mean that you have made a huge mistake - your date / time values in the DB are stored as strings, and they don't contain valid dates on your computer.

So start with the debugger, and look at exactly what is in that cell, and look at where that data comes from: if it is a DB, then you need to change the DB design and all the code that reads or writes to it. If you don't, this problem will come back, and back, and back ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900