I’m attempting to save a user’s date of birth into SQL Server 2008.
The web server is configured for en-AU format but SQL Server is configured to en-US (yeah, good stuff).
When I’m taking a date of birth in en-AU format such as:
24/12/1981
SQL Server is throwing an exception as that isn’t a valid date in en-US format.
How would I ensure that date of birth is stored in an en-US format? Is there an overload of DateTime.Now somewhere?
DateTimedoesn’t have a format as part of the value, any more thanintdoes. Asking whether the date of birth is storing in en-US format is like asking whether anintis stored in a hex or decimal format. You’re only seeing a format if you convert it to a string – which you shouldn’t be doing.Use parameterized SQL and set the value as a parameter, rather than building a SQL statement including the formatted version of the date, and all should be well. You shouldn’t be including the values directly in your SQL statement in general – for numeric and date/time values it gives conversion issues, and for text in particular it gives horribly security/escaping issues. Parameterized queries are cleaner, separating code from data, and avoiding unnecessary conversions.
EDIT: I see now that you’re using EF, in which case it should be building the parameteized query for you. It sounds like something you made need to get right in the connection string rather than anything else (although I’m disappointed that it’s an issue to start with). It’s definitely the SQL code that is complaining, rather than some part of MVC? It would help if you’d post a stack trace along with a SQL log.