String was not recognised as a valid DateTime

  • Hi i have a web app written in asp.net and c# but i am getting this error when i run my web app on a different laptop to the one i develop on and i cannot figure out why one pc gives the error and one doesnt, I guess its some SQL Server setting that differs on both pcs but i dont know how to find it. Any help of advice would really be appreciated

    Thanks in advance

    Tim

  • one sure way is to pass all datetimes in universal format

    yyyymmdd hhnnss (yearsmonthsdays hoursminutessecondes)

    instead of relying on SET DATEFORMAT that might change between language settings.

  • thanks for your reply but do you know how or where to go about doin this in C#

  • Hi,

    This is usually caused by differing Windows locales in the different SQL environments.

    To format the date in c# use the DateTime.ToString() method :

    DateTime MyDate = DateTime.Now;

    String FormattedDate = MyDate.ToString("yyyyMMdd hhmmss");

    You could also use the SQL Convert() function to explicitly cast the incoming date string to a specific locale formatted DateTime type. Lookup Convert() in BOL for more details.

    Mike.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply