开发者

ASP.NET Passing Inconsistent DateTime Format to SQL Server [duplicate]

This question already has answers here: RDLC - "String Not Recognized As A Valid DateTime" Error (2 answers) Closed 2 years ago.

I know this is an age-old question but I'm not exactly new to it and I haven't found an answer which helps me so far!

I've got an ASP.NET site which displays data from an SQL Server 2005 database. At the top of the page are 'Date From' and 'Date To' textboxes which are used to filter the data shown beneath.

The data is displayed using an SqlDataSource, and the two dates are passed as parameters to a Stored Procedure. The textboxes display the dates and accept input in UK date format (dd/MM/yyyy) and it works fine.

Now I've added a new page with exactly the same setup, displaying slightly different data. In the backend I created a new Stored开发者_开发问答 Procedure by copying and pasting my original one, it's almost identical. And yet on this page I get errors with my dates because they're being read as MM/dd/yyyy, meaning that today's date, for example, 15th August 2011, is passed as 15/08/2011 and isn't a valid date.

I've checked over everything and I can't understand why this should work on one page and not another, especially when I've basically just copied all of the original code and tweaked it slightly. Can anyone suggest anything I can check that I might not have thought of?


  • The text boxes are strings, which are in the thread local which is set from the browser (langauges).

  • Convert them to aa DateTime object first, using a local aware transformation, then write the dateTime object to the server. NEVER (!) deal with strings to sql server unless you format them in ISO independent form (2011-08-17 23:52:11).

But in general, ASp.NET will show dates, times, numbers in the local langauge of the browser. Either turn that off, or deal with it. It is nice for the user. So, check locales - user, server process. What is the thread current locale?


You need to use the SQL convert in stored procedure like:

convert(varchar, @yourdateParameter, 103) for format dd/mm/yyyy

or

convert(varchar, @yourdateParameter, 101) for format mm/dd/yyyy 

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜