CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Unable to resolve : Header with name 'FormId'[0] was not found.

Open ShashankDhasal opened this issue 3 years ago • 7 comments

I too am facing the same issue CsvHelper version 29.0

public static IEnumerable<T> ReadFromCsv<T>(string csv)
        {
            var conf = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                Delimiter = ",",
                HasHeaderRecord = true,
                TrimOptions = TrimOptions.Trim,
                MissingFieldFound = null
            };

            using (var stringReader = new StringReader(csv))
            using (var csvReader = new CsvReader(stringReader, conf))
            {
                return csvReader.GetRecords<T>().ToList(); // **gives exception Header with name 'FormId'[0] was not found.**
            }
        }

it is called like this ReadFromCsv<Form>(csvString).FirstOrDefault()

public class Form
	{
		public Int64 FormId { get; set; }
		public Int64 FormTypeId { get; set; }
	}

csvString = "FormId,FormTypeId 22,3"

ShashankDhasal avatar Oct 18 '22 11:10 ShashankDhasal

I did a dotnet fiddle here it works fine in fiddle but not on my dev machine

I am running on Windows 10, dotnet 6, CsvHelper 29.0

ShashankDhasal avatar Oct 18 '22 12:10 ShashankDhasal

Can you post the exception message you get? It contains pertinent details.

JoshClose avatar Oct 18 '22 15:10 JoshClose

Header with name 'FormId'[0] was not found. Headers: 'FormId', 'FormTypeId' 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.

IReader state: ColumnCount: 0 CurrentIndex: -1 HeaderRecord: ["FormId","FormTypeId"] IParser state: ByteCount: 0 CharCount: 2854 Row: 1 RawRow: 1 Count: 153 RawRecord: FormId,FormTypeId

ShashankDhasal avatar Oct 19 '22 05:10 ShashankDhasal

If I ignore the headers and skip the header row, then it works

var conf = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                HasHeaderRecord = false,
                TrimOptions = TrimOptions.Trim,
                MissingFieldFound = null,
                HeaderValidated = null
            };

            using (var stringReader = new StringReader(csvText))
            using (var csvReader = new CsvReader(stringReader, conf))
            {
                csvReader.Read();
                return csvReader.GetRecords<T>().ToList();
            }

ShashankDhasal avatar Oct 19 '22 05:10 ShashankDhasal

This works for me. Can you edit this example to make it fail?

void Main()
{
    var s = new StringBuilder();
    s.Append("FormId,FormTypeId\r\n");
    s.Append("22,3\r\n");
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
        Delimiter = ",",
        HasHeaderRecord = true,
        TrimOptions = TrimOptions.Trim,
        MissingFieldFound = null
    };
    using (var reader = new StringReader(s.ToString()))
    using (var csv = new CsvReader(reader, config))
    {
        csv.GetRecords<Form>().ToList().Dump();
    }
}

class Form
{
    public Int64 FormId { get; set; }
    public Int64 FormTypeId { get; set; }
}

image

JoshClose avatar Oct 19 '22 17:10 JoshClose

I have the same error message:

CsvHelper.HeaderValidationException: 'Header with name 'formId'[0] was not found.
Header with name 'formTypeId'[0] was not found.
Headers: 'FormId', 'FormTypeId'
Headers: 'FormId', 'FormTypeId'

But only if I add a constructor to the Form-class like this:

class Form
{
    public Form(long formId, long formTypeId)
    {
        FormId = formId;
        FormTypeId = formTypeId;
    }

    public Int64 FormId { get; set; }
    public Int64 FormTypeId { get; set; }
}

So the solution for now would be to delete the constructor or just ignore the header row.

Edit: It works if you also add an empty constructor. Otherwise the class can't get serialized.

rotsch avatar Nov 11 '22 09:11 rotsch

I had the same issue. My csv was UTF-8-BOM encoded.

Changing it to UTF-8 solved it for me..

Cororo avatar Mar 30 '23 14:03 Cororo