GetExceptionMessage fails when CSV file input doesn't have the correct number of columns #58

gromer opened this Issue Sep 24, 2012 · 4 comments


None yet
2 participants

gromer commented Sep 24, 2012

IndexOutOfBounds exception is thrown on line 901 of src/CsvHelper/CsvReader.cs:

error.AppendFormat( "Field Value: '{0}'", currentRecord[currentIndex] ).AppendLine();

To duplicate:


public class ImportObject
    [CsvField(Index = 0)]
    public int Id { get; set; }
    [CsvField(Index = 1)]
    public DateTime CreationDate { get; set; }
    [CsvField(Index = 2)]
    public string Description { get; set; }



My implementation:

IList<ImportObject> data = null;
using (var streamReader = new StreamReader(fileUpload.PostedFile.InputStream))
    using (var csvReader = new CsvReader(streamReader))
            csvReader.Configuration.HasHeaderRecord = false;
            data = csvReader.GetRecords<ImportObject>().ToList();
        catch (CsvReaderException cex)

Since there is no trailing comma on the first line of the sample CSV, currentRecord does not have a 3rd column, so the collection is only of count 2. GetExceptionMessage tries to access the data from that column after the import fails for the exception, and dies. It looks like this only happens if the first row of the CSV file doesn't have enough columns. I have not tried this with a CSV with a header row.

Dev Environment:
Windows 7 Professional SP1, x64
Visual Studio 2010 Professional, SP1
CsvHelper 1.8

Please let me know if there's any other information that would be helpful. Thanks for the great library!


JoshClose commented Sep 24, 2012

Good catch! I will fix that ASAP.


JoshClose commented Sep 24, 2012

I just committed a fix for this. f1dc941

It will but out with the next NuGet release.

JoshClose closed this Sep 24, 2012

gromer commented Sep 24, 2012

And you are awesome! Thanks for the mega quick response and fix, I'll get it when it's on NuGet. It wasn't something breaking my code right now, I was just testing out all possible mistakes the end user could make when creating a CSV file for my import code.


JoshClose commented Sep 24, 2012

Nice. If you find more, let me know! I surely haven't covered all cases in my unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment