C# code is reading data from database through dynamic queries.
Select ID, TransDate from Table_01
Business logic is processing the data and finally putting back the date into the database again.
INSERT INTO Table_02
( ID,ClosingDate) VALUES
( 1,Convert(DateTime, '27/07/2011 12:00:00 AM',120))
Since date format, i am inserting, is dd/MM/yyyy. Sql Server does not like it and .net throwing this error:-
The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value
If i change it to MM/dd/yyyy or yyyyMMdd then it works.
But system regional date time settings can be changed any time by any user, so I am looking for some concrete solution.
What is the best way to handle it?
Try setting the dateformat and using Cast: