Home > Software engineering >  Figuring out an issue using CSVReader with VB.NET
Figuring out an issue using CSVReader with VB.NET

Time:09-06

I am starting out using CSVHelper to write and read CSV files with VB.NET. I have been able to get writing of CSV files working, but am having an issue with the reading in of the file I just wrote.

The error message I am getting is: CsvHelper.HeaderValidationException: 'Header with name 'aFieldName'[0] was not found. Header with name 'afieldValue'[0] was not found.

If you are expecting some headers to be missing and want to ignore this validation, set the configuration HeaderValidated to null. You can also change the functionality to do something else, like logging the issue.


The file being read in has contents like this:

fieldName,fieldValue
Id,6f211f24-587e-4116-9d1d-8c8e0fd9e4bb
Name,Project Tracker

The code that I am using is this:

Public Class ClsInfoFields
    Property fieldName As String
    Property fieldValue As String
End Class
Friend Function ReadContentInfoCsv(aFilePath As String) As List(Of ClsInfoFields)
    Dim records As New List(Of ClsInfoFields)

    Dim config = CultureInfo.InvariantCulture
    Using r1 As New StreamReader(aFilePath)
        Using csvRead As New CsvHelper.CsvReader(r1, config)
            records = csvRead.GetRecords(Of ClsInfoFields).ToList
        End Using
    End Using

    Return records
End Function

Any suggestions on what I am missing or should try next?

CodePudding user response:

Use the `CsvConfiguration' class to configure the reader:

Dim config = New CsvConfiguration(CultureInfo.InvariantCulture) With {.HasHeaderRecord = False}
  • Related