Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CsvReaderException was unhandled

Tags:

c#

csvhelper

I keep running into this error:

An unhandled exception of type 'CsvHelper.CsvReaderException' occurred in CsvHelper.dll

Additional information: No properties are mapped for type 'RPS_String_Parse.Program+FormattedRow'.

But I believe I am following the documentation correctly. After referencing the "getting started" portion i implemented this:

using (var sr = new StreamReader(filePath))
{
    var csv = new CsvReader(sr);
    var records = csv.GetRecords<FormattedRow>();
    foreach (var record in records)
    {
        Console.WriteLine(record.Address1);
    }

    Console.ReadLine();
}

and my class:

public class FormattedRow
{
        public string IDOrderAlpha;
        public string IDOrder;
        public string AddressCompany;
        public string Address1;
        public string Address2;
        public string AddressCity;
        public string AddressState;
        public string AddressZip;
        public string AddressCountry;
        public string ShipMethod;
        public string ContactEmail;
        public string ContactName;
        public string ServiceRep;
        public string CustomerPuchaseOrder;
}

I feel like this should work, because the documentation states:

Auto Mapping

If you don't supply a mapping file, auto mapping will be used. Auto mapping will map the properties in your class in the order they appear in. If the property is a custom class, it recursively maps the properties from that class in the order they appear in. If the auto mapper hits a circular reference, it will stop going down that reference branch

What am I missing?

like image 610
drewwyatt Avatar asked Apr 30 '14 18:04

drewwyatt


2 Answers

The documentation states that it will map to Properties. Your class has Fields. Make this change:

public class FormattedRow
{
    public string IDOrderAlpha { get; set; }
    // add { get; set; } for all
}

This will change your fields to "auto properties".

like image 144
Jon B Avatar answered Oct 21 '22 12:10

Jon B


You need to set the configuration options for mapping:

var generatedMap = csv.Configuration.AutoMap<MyClass>();

So it appears you need to tell it to automap. I've never used this library before.

Edit: Jon B nailed it.

like image 27
David Crowell Avatar answered Oct 21 '22 14:10

David Crowell