Search code examples
c#stringcsvdatetimestring-conversion

Convert string to DateTime format issues C#


I have the below code and I get an error message that the Input string was not in correct format. Please help me

List<Crime> crimeList = new List<Crime>();
while (!reader.EndOfStream)
{
    var line = reader.ReadLine();
    var values = line.Split(',');
    crimeList.Add(new Crime()
    {
        Dates = DateTime.ParseExact(values[0],"yyyy-MM-dd HH:mm:ss",CultureInfo.InvariantCulture),
        Category = values[1],
        Description = values[2],
        DayOfWeek = values[3],
        PdDistrict = values[4],
        Resolution = values[5],
        Address = values[6],
        x = Convert.ToDouble(values[7]),
        y = Convert.ToDouble(values[8])
});

values[0] is 2015-05-13 23:53:00. https://s23.postimg.org/x0g4k37kr/datetime_problem.png


Solution

  • The exception was from this line x = Convert.ToDouble(values[7]) because from what I saw in your image, values[7] is a string of address :-?.