2

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?

0

1 Answer 1

9

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 )

Sign up to request clarification or add additional context in comments.

1 Comment

yes you are right. it is an old habit. thank you very much. I will mark it as soon as possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.