1

I wrote a program that does work with files like delete and update, store, and search And all customers,But the problem I keep getting the following error when I do an update of the file

String was not recognized as a valid DateTime.

Project File

Project Video

This error occurs in the Deserialize method:

        public Order Deserialize(string str)
    {
        Order order = new Order();
        var strOrder = str.Split(',');
        order.Id = int.Parse(strOrder[0]);

        **order.Date = DateTime.Parse(strOrder[1]);**
        order.Price = int.Parse(strOrder[2]);
        order.Description = strOrder[3];
        order.CustomerId = int.Parse(strOrder[4]);

        return order;
    }
3
  • What is the value of strOrder[1] ? Commented Nov 6, 2013 at 21:14
  • You have shown the string in the video, but did you check that strOrder[1] has the intended value? Can you add debug printing there? Commented Nov 6, 2013 at 21:16
  • The file is read and the string value example "2/5/2012". Commented Nov 6, 2013 at 21:16

2 Answers 2

1

Try Convert.ToDateTime

Edit per comment.

Import System.Globalization and try this:

var cultureInfo = new CultureInfo("en-US");
DateTime dateTime = Convert.ToDateTime(strOrder[1], cultureInfo);
Sign up to request clarification or add additional context in comments.

1 Comment

How about the other approach?
0

This is most likely to do with the culture settings. you need to specify the formats expected by using one of the overloads for the DateTime.ParseExact methods:

http://msdn.microsoft.com/en-us/library/332de853(v=vs.110).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.