I have a project created with VS2010. I am running the project from VS2008. Plz note that, I am not running the solution. I am only running the project. Fortunately the solution has only one project.
And in the following line I am having an exception.
List<Order> OrderList = new List<Order> {
new Order
{OrderID = 10248,
CustomerID = "VINET",
EmployeeID = 5,
OrderDate = DateTime.Parse("7/4/2006", CultureInfo.CreateSpecificCulture("en-US")),
RequiredDate = DateTime.Parse("8/1/2006", CultureInfo.CreateSpecificCulture("en-US")),
ShippedDate = DateTime.Parse("7/16/2006", CultureInfo.CreateSpecificCulture("en-US")),
ShipVia = 3, Freight = 32.3800M, ShipName = "Vins et alcools Chevalier",
ShipCountry = "France",
Order_Details = new List<Order_Detail>(),
Customer = new Customer(),
Employee = new Employee(),
Shipper = new Shipper()}};
The exception is:
FormatException was unhandled:
String was not recognized as a valid DateTime.
Stack Trace is:
NwindObjectsCS.exe!NwindObjectsCS.frmMain.CreateOrderList() Line 142 C#
NwindObjectsCS.exe!NwindObjectsCS.frmMain.btnInitializer_Click(object sender = {Text = “Load (&Initializer)”}, System.EventArgs e = {X = 86 Y = 22 Button = Left}) Line 51 + 0xe bytes C#
[External Code]
NwindObjectsCS.exe!NwindObjectsCS.Program.Main() Line 18 + 0x1d bytes C#
[External Code]
What suppose to be the problem?
I have downloaded the code from WROX Web site. The code is from the book “Professional ADO.NET 3.5 with LINQ and the Entity Framework”. Author is “Roger Jennings”. So there should not be any problem.
It is from 3rd chapter.
I tried this (My machine is set to “en-GB”):-
I get no errors. I then go into control panel, regional settings, customize, change my short date format to yyyy-mm-dd and re-run the prog. It then throws the same exception.
Switching the locale completely doesn’t affect it though.
It seems that if you’ve customised your regional settings for the region specified then it breaks.
Have you customised the “en-US” regional date settings on your machine?
Alternatives.
ParseExact:-
Kobi’s suggestion:-