Reputation: 5876
I keep having the same problem in my c# projects. I try to insert datetime to sql table, but in Turkey. the date is always DD.MM.YYYY, C# or SQL sometimes try to insert MM.DD.YYYY like American way.
How do I solve this problem forever?
here is an example code
string selectSql = @"INSERT INTO [RN_VEHICLES_GUEST_INOUT] ([PLAKA],[MY_DATE])
VALUES(@PLAKA,@MY_DATE)
cmd.Parameters.Add("@PLAKA", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@GELIS_TARIHI", SqlDbType.DateTime);
cmd.Parameters["@PLAKA"].Value = txtPlaka.Text.ToString();
cmd.Parameters["@GELIS_TARIHI"].Value = dateGelisTarihi.EditValue.ToString();
This code worked until today, It crashes today because today is 13.07.2013 and it tries to insert it as MM.DD.YYYY. yesterdat it was 12.07.2012 and I didnt have problem because it could convert 12 as a month but not 13. thats how I realized the problem.
How can I make sure C# and SQL always work in DD.MM.YYYY format?
Upvotes: 2
Views: 2147
Reputation: 460268
If it's a DateTime
field in database and a DateTime
variable in C#, why do you convert it to a String
at all?
So instead of
cmd.Parameters.Add("@GELIS_TARIHI", SqlDbType.DateTime);
cmd.Parameters["@GELIS_TARIHI"].Value = dateGelisTarihi.EditValue.ToString();
just
cmd.Parameters.Add("@GELIS_TARIHI", SqlDbType.DateTime);
cmd.Parameters["@GELIS_TARIHI"].Value = dateGelisTarihi.EditValue;
( assuming that dateGelisTarihi.EditValue
is a Dateime
)
Upvotes: 9