Fields 'workOrder' do not exist in the CSV file

haosmark picture haosmark · Feb 10, 2014 · Viewed 7.8k times · Source

I have a CSV file with several rows and each row has a work order number under column titled "W.O.No."

Here's what I'm doing: I created a class called Job, with a single field.

class Job
{
    public int workOrder { get; set; }
}

Then I created a JobMap class

class JobMap : CsvClassMap<Job>
{
    public override void CreateMap()
    {
        Map(m => m.workOrder).Name("W.O.No.");
    }
}

And finally, this is my main code

StreamReader file = new StreamReader(filePath);
var csv = new CsvReader(file);
var record = csv.GetRecords<Job>().ToList();

For whatever reason I'm getting an error saying

Fields 'workOrder' do not exist in the CSV file.

What am I doing wrong here?

Answer

Stadub Dima picture Stadub Dima · Feb 10, 2014

Seems that in you code missing map initialization. Try to add csv.Configuration.RegisterClassMap<JobMap>();

This code should work:

StreamReader file = new StreamReader(filePath);
var csv = new CsvReader(file);
csv.Configuration.RegisterClassMap<JobMap>();
var record = csv.GetRecords<Job>().ToList();