CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

MissingFieldFund = Null not working proper;y

Open xirtam2669 opened this issue 3 years ago • 1 comments

Hi Josh, I've started using your library today and am running into a problem. I have a CSV with lots of whitespace. I'm using MissingFieldFound and still getting an exception stating:

"Unhandled exception. CsvHelper.MissingFieldException: Field at index '2' does not exist. You can ignore missing fields by setting MissingFieldFound to null."

Am I missing something? My code is :

static object ReadCsv(string csvFileName)
    {
        var config = new CsvConfiguration(CultureInfo.InvariantCulture)
        {
            HasHeaderRecord = false,
            MissingFieldFound = null
        };
        using (var reader = new StreamReader(csvFileName))
        using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
        {
            var records = csv.GetRecords<CSV>().ToArray();
            Console.WriteLine(records.ToString());
        }

        return 0;
    }

xirtam2669 avatar Jul 27 '22 21:07 xirtam2669

You need to replace new CsvReader(reader, CultureInfo.InvarientCulture) with new CsvReader(reader, config).

AltruCoder avatar Jul 28 '22 01:07 AltruCoder