The issue I have is with the CSVHelper library in particular.
My csv file looks something like this:
Number,Date,Account,Amount,Subcategory,Memo
,09/05/2017,XXX XXXXXX,-29.00,FT , [Sample string]
,09/05/2017,XXX XXXXXX,-20.00,FT ,[Sample string]
,08/05/2017,XXX XXXXXX,-6.30,PAYMENT,[Sample string]
What I am doing with CSVHelper is this:
List<Transaction> result = new List<Transaction>();
using (TextReader fileReader = File.OpenText("data.csv"))
{
var csv = new CsvReader(fileReader);
result = csv.GetRecords<Transaction>().ToList();
}
The issue is that when It tries to execute GetRecord on the last line, I get this exception:
Unhandled Exception: System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.DateTime.Parse(String s, IFormatProvider provider, DateTimeStyles styles)
at CsvHelper.TypeConversion.DateTimeConverter.ConvertFromString(TypeConverterOptions options, String text)
at lambda_method(Closure )
at CsvHelper.CsvReader.CreateRecord[T]()
at CsvHelper.CsvReader.<GetRecords>d__65`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
As you can see the data has some issues - the first column is either empty or has the string null. Even then, the exception message points to a problem with the Date which is the second column.
Any help would be appreciated.
My guess is that your default culture specifies a different date format than what is in the CSV file. Try setting the culture to one that matches the data.
var csv = new CsvReader(fileReader);
csv.Configuration.CultureInfo = CultureInfo.GetCultureInfo("en-GB");
result = csv.GetRecords<Transaction>().ToList();
Fiddle: https://dotnetfiddle.net/iTvc4Y