Another String was not recognised as a valid DateTime And XML Element



I have an XML element which contains data in the following DATE format (the XML data is from a third party web service so i have no control over the data returned)



1/12/2012 and
2012


I am getting the error


String was not recognised as a valid DateTime


So i read around and tried (My C# isnt as efficient as VB .Net)



DateOfBirth = DateTime.TryParse(c.Element(ns + "date-span").Elements(ns + "begin").Any() ? c.Element(ns + "date-span").Element(ns + "begin").Value : DateTime.Now.ToString(), DateTime.Now.ToString());


but whatever i try i either get a syntax error or string was not recognised.


The type of DateOfBirth in my SQL DB is Datetime - but changing this to NVARCHAR resolves the problem but this means i cant store it as a valid datetime format to perform datetime operations once the data is in the database.


Is there a way to overcome the above error or format 2012 to 01/01/2012? Or is the only way to do this is to store it as a NVARCHAR and then Convert.ToDateTime(valueFromDatabase) when i need to do operations on the date that has been saved?


No comments:

Post a Comment