shanish
shanish

Reputation: 1984

DateTime conversion - server

I've declared my DateOfBirth property as string and am parsing it to DateTime before passing it to the database, coz the DateOfBirth column is in DateTime format there.

in my local system, the conversion is working fine, but after hosting the application to the server, the DateTime conversion showing error like "string was not recognized as a valid DateTime."

I've tried with

 <system.web>
  <globalization culture="en-gb"/>
  <!-- ... -->
</system.web>

and

DateTime.Parse(DateOfBirth, new CultureInfo("en-GB"))

no use, whats this issue, can anyone help me here...

Upvotes: 0

Views: 640

Answers (3)

Sas
Sas

Reputation: 2503

I had the same prob, I changed the culture info in web.config file as:

<globalization culture="en-gb" uiCulture="auto"/>

OR

try parsing your string to date as:

DateTime dob = DateTime.ParseExact(DateOfBirth, "dd/MM/yyyy", 
CultureInfo.InvariantCulture);

hope it helps... :)

Upvotes: 2

Eric Andres
Eric Andres

Reputation: 3417

My guess would be that according to the culture, you are passing in an invalid date. Generally, British dates are written with the day first, then the month. You're passing in 4/13/1988, which parses out to the 4th day of the 13th month. What month is 13? I don't know, and neither does the .Net Runtime.

Upvotes: 4

Mathew Thompson
Mathew Thompson

Reputation: 56429

You need to convert it, something like:

var sqlDate = myDateTime.ToString("yyyy-MM-dd HH:mm:ss");

Upvotes: 2

Related Questions