Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting the following error:
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

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

I am getting the error in the code mentioned in bold

The following is the code:
    private void LoadData()
{
    con.dataGet("Select * from [User]");
    DataTable dt = new DataTable();
    con.sda.Fill(dt);
    foreach(DataRow row in dt.Rows)
    {
        int n = dataGridView1.Rows.Add();
        dataGridView1.Rows[n].Cells["dgSNo"].Value = n + 1;
        dataGridView1.Rows[n].Cells["dgName"].Value = row["Name"].ToString();
        dataGridView1.Rows[n].Cells["dgDob"].Value = Convert.ToDateTime(row["Dob"].ToString()).ToString("dd/MM/yyyy");                dataGridView1.Rows[n].Cells["dgBranch"].Value = row["Branch"].ToString();
        dataGridView1.Rows[n].Cells["dgRole"].Value = row["Role"].ToString();
        dataGridView1.Rows[n].Cells["dgESICNo"].Value = row["ESICNo"].ToString();
        dataGridView1.Rows[n].Cells["dgPFNo"].Value = row["PFNo"].ToString();
        dataGridView1.Rows[n].Cells["dgAddress"].Value = row["Address"].ToString();
    }
}


What I have tried:

I have tried to change the format of the date
Posted
Updated 11-Mar-21 22:28pm
Comments
Garth J Lancaster 14-Aug-20 2:15am    
What database data-type is dgDob in the User table ?

Why on earth have you written this:
C#
dataGridView1.Rows[n].Cells["dgDob"].Value = Convert.ToDateTime(row["Dob"].ToString()).ToString("dd/MM/yyyy");

Take a cell content.
Convert it to a string.
Convert that to a DateTime.
Convert that to a string
Put that in a DGV cell.

It's a lot easier - and more code friendly - to keep everything as DateTime objects and use the DGV formatting facilities:
C#
dataGridView1. Columns["dgDob"].DefaultCellStyle.Format = "dd/MM/yyyy";


But the error you show we can't help you with directly - it's a data based problem and we have no access to your DB.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

And if you are doing what you are because the database holds strings stored as dates ... change your DB so it uses DATE, DATETIME, or DATETIME2 instead! That'll be a paint (because I suspect you've already got corrupt dates in there) but it's a lot easier in the long run.

And ... You do realise that you could just use the SELECT to return all the columns you want in the right order and content then just use it as the DataSource instead of a manual loop to load the data? Again, much easier and more maintainable.
 
Share this answer
 
You are getting a format exception. Seems it expects a datetime but the value passed is not of valid datetime format.
Quote:

FormatException

value is not a properly formatted date and time string.

Refer: FormatException Class (System) | Microsoft Docs[^]

For here:
dataGridView1.Rows[n].Cells["dgDob"].Value = Convert.ToDateTime(row["Dob"].ToString()).ToString("dd/MM/yyyy");  

1. Is Cells["dgDob"].Value setup to expect a datetime (just confirming)
2. Is row["Dob"].ToString() really having a date data? (hope its not empty or any string which cannot be used as a date). Would suggest you to use: DateTime.TryParse Method (System) | Microsoft Docs[^]
3. For datetime formats of string, look here: String Format for DateTime [C#][^]

Also, if you want to convert any format of date to dd-MM-yyyy, try:
DateTime.ParseExact("YouDateString", "dd-MM-yyyy", CultureInfo.InvariantCulture)


BTW, for your error above, mostly you need to look into point 2 above.
 
Share this answer
 
Just look which string will be returned by
row["Dob"].ToString()
The result of 
ToString("dd/MM/yyyy")
is never used (that is the return value of Convert.ToDateTime).
Use your debugger!
 
Share this answer
 

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



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