I have a c# winforms app which is databound to table with multiple dates. The client requires that date can by inputted using a text-box. However, I cannot get it to work like it should. The problem is when I leave the textbox the day and month change places. So when I type 9-12-1950 it will show up as 12-09-1950. Same when I type 09-12-1950. Internally the date is stored as yyyy-MM-dd. Databinding is enabled using the format dd-MM-yyyy. I use the validating event t0 check if the date is valid like:
if (DateTime.TryParseExact(tbDate.Text, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue))
tbDate.Text = String.Format("{0:dd-MM-yyyy}", dateValue);
When I debug I see that the text-value of tbDate is still in the right format, in my case 9-12-1950 for example. I also see that the dateValue which is returned from the TryParseExact holds the same date, but in the format of MM/dd/yyyy.
Saving the date to the database is set onValidation. What am I missing here?
[Update] Maybe I was not clear enough on my explanation. The date is stored in the SQL Db in the format yyyy-MM-dd of type DateTime (not varchar). On my form I want to show and edit it as dd-MM-yyyy (There are some other formats too like: "dd/MM/yyyy", "dd/M/yyyy", "d/M/yyyy", "d/MM/yyyy", "dd/MM/yy", "dd/M/yy", "d/M/yy", "d/MM/yy","dd-MM-yyyy", "dd-M-yyyy", "d-M-yyyy", "d-MM-yyyy","dd-MM-yy", "dd-M-yy", "d-M-yy", "d-MM-yy".
The typing is working, but as soon as I move away from the TextBox the day and month change place. I think I miss something. Just don't know what.
I feel a little stupid, the problem was caused by the local system setting, which was set to M/d/yyyy. I changed it to my local culture dd-MM-yyyy which solved the problem.