Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone,

I'm getting an error when edition data using the the foreach command to populate a
table, the error is with the Datatime only .

this syntax is not working for me :

dtpValideUtilisation.Text = convert.ToDateTime(dr["date_valide_utilisation"]).ToString("dd/MM/yyyy");

What I have tried:

foreach (DataRow dr in dt.Rows)

{
textCarUser.Text = dr["caruser"].ToString();
textEngagement.Text = dr["engagement"].ToString();
dtpValideUtilisation.Text = convert.ToDateTime(dr["date_valide_utilisation"]).ToString("dd/MM/yyyy");
textNum_Serie.Text = dr["num_serie"].ToString();
textType.Text = dr["type"].ToString();
}
Posted
Updated 29-Aug-21 6:58am
Comments
Richard MacCutchan 29-Aug-21 8:24am    
What is the value of dr["date_valide_utilisation"]?
Sino123 29-Aug-21 9:29am    
It's the column name in the db
Richard MacCutchan 29-Aug-21 9:32am    
Well that is pretty obvious, even to me. But what is the value held in that column?
Sino123 29-Aug-21 10:22am    
it's a date (dd/MM/yyyy)
Richard MacCutchan 29-Aug-21 10:25am    
What is its actual value?

Until you know the actual value in the cell you cannot tell whether it is being converted correctly. And if it is already a string in the form "dd/MM/yyyy" why on earth are you trying to convert it into a DateTime, just so you can convert it back to a string?

As explained in Solution 1, chances are that when looping through rows, one or more rows do not contain a proper date. Another possible situation that you don't have a date at all, meaning that on some rows thee value is NULL. If this is possible it should be taken into account.

In my opinion the root cause is that you store dates as text in the data table. Instead you should use DateTime as the column type for date_valide_utilisation. By doing this you don't need to convert the dates at all.

Another thing is that it looks like you're using a datetimepicker. If that's true, try using value property instead of text property. Have a look at DateTimePicker.Value Property (System.Windows.Forms) | Microsoft Docs[^]

By doing these two changes (column data type and value property) you can remove all text conversions and place the datetime from the datarow directly to the control and vice versa.
 
Share this answer
 
I would use DateTime.TryParse to transform the value into a date. My suspicion is the value you are trying to transform is not always a date in the underlying data.
 
Share this answer
 
Comments
Sino123 29-Aug-21 9:30am    
what will be the syntax please!
Pete O'Hanlon 29-Aug-21 10:25am    
There are plenty of examples here. https://www.google.com/search?q=datetime+tryparse&oq=datetim.tr&aqs=chrome.2.69i58j69i57j0i10i30.7243j0j7&client=ms-android-samsung-gs-rev1&sourceid=chrome-mobile&ie=UTF-8

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