views:

523

answers:

6

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.

+1  A: 

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?

Psytronic
Don't USA use mm/dd/yyyy format?
JMSA
It shows "en-US".
JMSA
then I agree with Virgil, it's got to be with some constructor in your Customer, Employee or Shipper class
Psytronic
I have downloaded the code from WROX Web site.
JMSA
The code is from the book "Professional ADO.NET 3.5 with LINQ and the Entity Framework". author is "Roger Jennings".
JMSA
So there should not be any problem.
JMSA
Which chapter is the code from?If you comment out/remove the contsructors does it work?
Psytronic
it is from 3rd chapter.
JMSA
Yes commenting out works.
JMSA
Which one causes it to break? Ie comment out one at a time
Psytronic
The entire line goes to a break. And pressing F11(Step into) doesn't go forward.
JMSA
If u have the code downloaded, u can find it in TestClass.cs->public List<Order> CreateOrderList(){/...}.
JMSA
Yeah, I have, and it runs fine for me (Visual C# Express 08), so harder to work out which bit is breaking it.
Psytronic
OK. Nothing to say more. lol
JMSA
The only thing I did have to do is to apply the CultureInfo.CreateSpecificCulture("en-US") to previous DateTime parses, but that is because I am UK based.
Psytronic
Only way I can think of pinpointing it is to block comment out every line of the function and go through one by one, if they all fail then there is something major wrong, if it's only failing on one line then we need to look at that one line in more detail, as there are 99lines in that function it maybe long process.
Psytronic
A: 

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

dutt
But why I need this? Can u explain a little?
JMSA
And it didn't work. Sorry!
JMSA
+2  A: 

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?)

Virgil
That is correct, all three `DateTime.Parse` run well.
Kobi
A: 

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()}};
ck
I have downloaded this code from the WROX website. I don't know why the author has chosen to do this.
JMSA
+1  A: 

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"))
Simpzon
+2  A: 

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));
JonB
OK. This solved my problem finally.
JMSA
This is even in the documentation. I didn't know that one. Sounds horrible. It seems there's a second parameter you can set for that, you may want to add it to your answer.
Kobi
This turns out that, this problem is completely unrelated to coding.
JMSA
@JMSA, I'm not sure about that, from the MSDN page on the cultureInfo:-"Culture properties are meant for display, not for round-tripping of data."They seem to be saying not to do this with it..@Kobi, Do you mean DateTimeStyles? I don't think that helps.
JonB
@JMSA: Try using `ParseExact` rather than `Parse`. This will allow you to specify the exact format of the date/time string. http://msdn.microsoft.com/en-us/library/system.datetime.parseexact.aspx
LukeH
@Jon - sorry, I referred to this: http://msdn.microsoft.com/en-us/library/7h898a16.aspx
Kobi
Thanks, added those in for completeness.
JonB