Question

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.

Was it helpful?

Solution

I tried this (My machine is set to "en-GB"):-

static void Main(string[] _args)
{
  DateTime d1 = DateTime.Parse("7/4/2006", CultureInfo.CreateSpecificCulture("en-GB"));
  DateTime d2 = DateTime.Parse("8/1/2006", CultureInfo.CreateSpecificCulture("en-GB"));
  DateTime d3 = DateTime.Parse("16/7/2006", CultureInfo.CreateSpecificCulture("en-GB"));
  System.Console.WriteLine("d1:" + d1.ToString());
  System.Console.WriteLine("d2:" + d2.ToString());
  System.Console.WriteLine("d3:" + d3.ToString());
  System.Console.ReadKey();
}

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:-

DateTime.ParseExact("16/7/2006", "d/M/yyyy", CultureInfo.InvariantCulture);

Kobi's suggestion:-

DateTime.Parse("16/7/2006", new CultureInfo("en-GB", false));

OTHER TIPS

If you launch it in the debugger, it should show you exactly what caused the exception. On a quick glimpse, your date strings seem to be valid for the en-US locale, I can only suspect the issue to be somewhere else (e.g. in the constructor of one of your objects?)

I should think that it is due to your current globalisation settings, and the 7/16/2006 date, and it thinking it is in dd/mm/yyyy format, as the 16th month isn't valid.

At the beginning of your script run this line:

MessageBox.Show(CultureInfo.CurrentCulture.ToString());

What does it show?

Maybe you have set some uncommon DateFormat specified in your OS regional settings, which is adopted in the CultureInfo you create. To find out about that try what you get when running

new DateTime(2006,7,4).ToString();

I can't try on my system, but maybe you can get a "default" CultureInfo without your customizations with

OrderDate = DateTime.Parse("7/4/2006", new CultureInfo("en-US"))

Why are you parsing a hardcoded datetime rather than constructing it directly:

List<Order> OrderList = new List<Order> {
new Order 
{OrderID = 10248, 
CustomerID = "VINET", 
EmployeeID = 5, 
OrderDate = new DateTime(2006, 7, 4),
RequiredDate = new DateTime(2006, 8, 1),
ShippedDate = new DateTime(2006, 7, 16),
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()}};

Think you need to add a time as well, try adding "07:00 AM" to the date-strings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top