Click here to Skip to main content
15,070,613 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?
Sino123 29-Aug-21 10:51am
   
What should I do then as I'm stocked, I've tried dtpValideUtilisation.Text = (dr["date_valide_utilisation"]).ToString(); but it didn't work
Richard MacCutchan 29-Aug-21 10:56am
   
"but it didn't work"
We cannot guess what happens when you run your code. We have no idea what data exists in your table, or what format it is in. Unless you provide complete details we have no way of helping you. So show us exactly what happens when you try to extract the contents of that cell.
Wendelius 29-Aug-21 8:38am
   
And what does convert.ToDateTime do, is it your own class/method?
Sino123 29-Aug-21 10:58am
   
nop its not, I've changed it to this one and I kept getting an error message dtpValideUtilisation.Text = (dr["date_valide_utilisation"]).ToString;

I don't really know where is the problem ... I'm stocked!

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.
   
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.
   
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