Header with name 'fieldname' was not found.
I'm trying to use the CsvHelper with the code below:
var reader = new CsvReader(sr);
IEnumerable<DataRecord> records = reader.GetRecords<DataRecord>();
foreach (DataRecord record in records)
{
//Do stuff with the record
}
When I'm using version 2.0.0.0 it's working like a charm. I used this version by downloading an example online. But when I use the latest version 12.0.0.0 I'm getting the error below on the foreach line:
Header with name 'CommonName' 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.
I tried following the documentation but I'm always getting a similar error. The CSV file and class file all fields have an identical name. Can someone explain to me why this isn't working anymore in the latest version?
Do you have a property named CommonName that doesn't have a corresponding field in the CSV file?
The CSV file contains the following data:
CommonName,FormalName,TelephoneCode,CountryCode
Argentina,Argentine Republic,54,ARG
Armenia,Republic of Armenia,374,ARM
Australia,Commonwealth of Australia,61,AUS
The DataRecord class:
public class DataRecord
{
public String CommonName { get; set; }
public String FormalName { get; set; }
public String TelephoneCode { get; set; }
public String CountryCode { get; set; }
}
As far as I can see all requirements and fields are present.
This seems to work fine for me.
void Main()
{
var s = new StringBuilder();
s.AppendLine("CommonName,FormalName,TelephoneCode,CountryCode");
s.AppendLine("Argentina,Argentine Republic,54,ARG");
s.AppendLine("Armenia,Republic of Armenia,374,ARM");
s.AppendLine("Australia,Commonwealth of Australia,61,AUS");
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader))
{
csv.GetRecords<DataRecord>().ToList().Dump();
}
}
public class DataRecord
{
public String CommonName { get; set; }
public String FormalName { get; set; }
public String TelephoneCode { get; set; }
public String CountryCode { get; set; }
}

This also seems to work fine.
void Main()
{
var s = new StringBuilder();
s.AppendLine("CommonName,FormalName,TelephoneCode,CountryCode");
s.AppendLine("Argentina,Argentine Republic,54,ARG");
s.AppendLine("Armenia,Republic of Armenia,374,ARM");
s.AppendLine("Australia,Commonwealth of Australia,61,AUS");
using (var reader = new StringReader(s.ToString()))
using (var csv = new CsvReader(reader))
{
IEnumerable<DataRecord> records = csv.GetRecords<DataRecord>();
foreach (var record in records)
{
record.Dump();
}
}
}
public class DataRecord
{
public String CommonName { get; set; }
public String FormalName { get; set; }
public String TelephoneCode { get; set; }
public String CountryCode { get; set; }
}

There must be something else that isn't represented in these examples.
Not sure if it is the same issue, but I got exactly the same error for the header column 'fieldname' that was not at all in the file I was reading.
It turned out that the problem was that my class being read was missing the default constructor (I only had a constructor with parameters). Perhaps it would be a good idea to add a where T : new() constraint for the GetRecords
So i had this exact same issue, and it turned out that the problem was the seperating character registered on my system.
I'd been using the pipe character so much, for other csv work on other things, that it got annoying dealing with excel and telling it to use | everytime i wanted to process a pipe delimmited CSV. So i went to my PCs language settings, and set the list seperator to be the pipe character, by default.
Since this library reads that setting from the PC's system settings, by default, it was trying to parse my file, thinking the field seperator to use was pipe. For this particular csv, it was comma. So, i just had to do
csv.Configuration.Delimiter = ",";
before calling
csv.GetRecords.
Might be what's wrecking you.
Hey, I just wanted to ask a question related to this problem .
I have my header in CSV file looking like this Client_Id,Request_id,Name,Quantity,Price
and as long as all CSV headers will look like that - everything is fine.
But, I want to handle the situation in which the user uploads csv with wrong headers like client_Id,request_id,name,quantity,price.
What exception should I try to catch when this happens?
@andrewpajda, you just need to use csv.Configuration.PrepareHeaderForMatch = (header, index) => header.ToLower(); to be able to ignore the case of the headers. You can find this and other helpful tips in the Getting Started Section,
This I understand. But what if for some reason, headers dont match entirely. For example user uploaded different csv by mistake. So there are more columns and they represent completely different data.
@andrewpajda I think that specific issue might be related to #1032. It doesn't look like there is a good way to tell if there are extra columns that you weren't expecting.
Hi all... Believe it or not... I could read a CSV well on linux with dotnet core 2.2.105 but running the same code on Windows 10 with 2.2.202 is not working and I get the following error !
``2019-04-25 18:39:28,860 [5] ERROR Standard [(null)] - Header with name 'idCentro' 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.
CsvHelper.HeaderValidationException: Header with name 'idCentro' 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.
at CsvHelper.Configuration.ConfigurationFunctions.HeaderValidated(Boolean isValid, String[] headerNames, Int32 headerNameIndex, ReadingContext context)
at CsvHelper.CsvReader.ValidateHeader(ClassMap map)
at CsvHelper.CsvReader.GetRecords[T]()+MoveNext()
at coreproxy.Controllers.WebServiceTesterController.CargarPosicionesCSV() in E:\artexacta\proyectos\WMS\codigo\coreproxy\Controllers\WebServiceTesterController.cs:line 90
I'm using: CsvHelper" Version="12.1.2 What is the problem ????
Probably your delimiter. Explicitly set it and see if that works.
csv.Configuration.Delimeter = ",";
Hello, I have the EXACT same problems. My properties are well named, I had de Delimiter options and anyway it does not work. @Mattias85 did you solved it ?
Thanks.
@julioagp Can you provide a failing example?
I battled this exact error for two days. Mapped the file nothing changed. At one point I had the CSVReader working without error but all the fields were blank when they should not be. The answer in the end was that I was missing a default parameterless constructor. Hope this comment helps someone else save time :)
Try using ReadHeader as below:
using (var stringReader = new StringReader(str.Trim()))
using (var csvReader = new CsvReader(stringReader))
{
csvReader.Read();
csvReader.ReadHeader();
var compoents = csvReader.GetRecords<Type>();
}
I'm having the same issue as originally described. I have the following
//Data
B S U,Episode Row,Street Address,City,State,Zip, ...
12345678,1,123 My Street,SomeCity,WI,53186, ...
98765432,1,321 Street 2,Anothercity,WI,53186,...
98765432,2,321 Street 2,Anothercity,WI,53816,...
//Simplified version of what I'm doing
byte[] fileBytes = File.ReadAllBytes(@"c:\temp\myepisodefile.csv");
string fileText = Encoding.Default.GetString(fileBytes);
using(var reader = new StringReader(fileText))
{
using(var csv = new CsvReader(reader))
{
csv.Configuration.RegisterClassMap<EpisodeMap>();
csv.Configuration.MissingFieldFound = null;
var episodes = csv.GetRecords<Episode>().ToList();
}
}
public class EpisodeMap : ClassMap<Episode>
{
public EpisodeMap()
{
Map(e => e.ReferenceNumber).Name("B S U");
Map(e => e.ReferenceId).Name("Episode Row");
Map(e => e.AddressLine1).Name("Street Address");
}
}
public class Episode
{
public string ReferenceNumber {get; set;}
public string ReferenceId {get; set;}
public string AddressLine1 {get; set;}
public string AddressLIne2 {get; set;}
...
}
Whenever I hit the line var episodes = csv.GetRecords<Episode>().ToList(); I get the error
Header with name 'B S U' was not found.
If I comment out the line Map(e => e.ReferenceNumber).Name("B S U");, I get no error and everything works. However, if I set a breakpoint on the GetRecords<Episode>() line (and catch it before it happens) and inspect fileText, I can see B S U, as the first characters in the text.
If I then go to my Immediate Window and perform csv.Read(); followed by csv.ReadHeader(); and then inspect csv.Context.HeaderRecord[0], I can see "B S U".
However, when I try "B S U" == csv.Context.HeaderRecord[0], I get False. I just did an ASCII dump of csv.Context.HeaderRecord[0] and the first ASCII character is 65279 -- which I found is a Byte Order Mark. It would appear that this is what is causing my problem. Now I just need to figure out how to get rid of this.
Hope this helps someone else as well!
Hello everyone,
I had the same problem without being able to solve it for a couple of days, I followed several post threads to find a solution and I found my solution when opening the document as a plain text file in note pad and I realized that one of the HEADER had a space at the beginning " MAX_VALUE "and" MIN_VALUE "this caused me the error of not finding a column.
In the CSV file remove those spaces in the ** header ** that by a finger error were being generated like that with those spaces.
I think that another solution may also be to add the "Attribute" of the CsvHelper to tune the header and to find the "N" columns of the file.
[CsvHelper.Configuration.Attributes.Name ("MAX_VALUE")]
Join the theme of Delimetter "," but if I was separating correctly, however the aforementioned mentioned me.
I hope someone is helpful, this worked for me.
@roddharris You should be able to fix that by setting up your StreamReader properly. CsvHelper just reads the text you give it.
@davideveloper You can also use csv.Configuration.TrimOptions = TrimOptions.Trim; to automatically trim your fields. I would only suggest this if you don't have control over your data and it has a lot of extra whitespaces.
@JoshClose
I'll try why not.
I already solved it by modifying the file I am going to read but to avoid future problems, your point is good.
Thank you!