Why would dateTimePicker.Value.Date.ToShortDateString();
act differently on Windows 7 x64 PL, Windows Vista x32 PL and Windows XP PL with to my knowledge exact regional settings. I've found it out the hard way that i was doing this conversion prior to entering it to DB.
It was working fine on Windows 7 (my development machine), colleague VISTA system but it failed to work on Windows XP (day was switched with month all the time). Also on higher systems we have 2010-01-13 displayed on ListView
while on his system he has 13-01-2010.
I imagine in my old code i may have more of those type conversions and i will have to go thru and verify it but i would like to know why it's behaving that way on same regional settings. I imagine I should never do conversions like that but I've learn it the hard way after a long time when it was working fine.
EDIT:
I was using it this way (commented out code that was causing troubles). Back in the old days I thought ToShortDateString was the only way to make sure to put it into DB without Time (since i was reading DateTimePicker). I know now that I should have used .Date on that DateTimePicker but I am smarter now that it did blow up on me. Here's the code:
private static void sqlWpiszDaneSwieta(DateTime varData, string varDataNazwa) {
//string varDataSwieto = varData.ToShortDateString();
const string preparedCommand = @"INSERT INTO [dbo].[TypyDatySwiat]
([SwietaData]
,[SwietaNazwa])
VALUES
(@varData
,@varDataNazwa)";
using (var varConnection = Locale.sqlConnectOneTime(Locale.sqlDataConnectionDetails))
using (SqlCommand sqlWrite = new SqlCommand(preparedCommand, varConnection)) {
sqlWrite.Prepare();
sqlWrite.Parameters.AddWithValue("@varData", varData);
sqlWrite.Parameters.AddWithValue("@varDataNazwa", varDataNazwa);
try {
sqlWrite.ExecuteNonQuery();
} catch (SqlException sqlEx) {
if (sqlEx.Message.Contains("Violation of PRIMARY KEY constraint")) {
MessageBox.Show("Dodanie podanego święta jest niemożliwe. Podane święto istnieje już w bazie danych!", "Bład", MessageBoxButtons.OK, MessageBoxIcon.Error);
} else {
MessageBox.Show(sqlEx.ToString(), "Bład SQL", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} catch (Exception ex) {
MessageBox.Show(ex.ToString(), "Bład", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
So i am not specifically asking for a way to do it. I know how to do it and that i can pass it using DateTime directly to db. Just that i would like to know why would it behave differently on 1 machine.