Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# CsvHelper.ValidationException - Why?

Tags:

c#

csvhelper

I am trying to use CSVHelper in C# console app . I have a this exception:

CsvHelper.ValidationException: 'Header matching ['Numer Dokumentu'] names at index 0 was not found.

And I don't know why becouse this header is on his place in csv file.

Here is my Program.cs

var packs = new List<Pack>();
            using (var streamReader = File.OpenText("C:/.../file.csv"))
            {                
                var reader = new CsvReader(streamReader);
                reader.Configuration.RegisterClassMap<PackMap>();
                packs = reader.GetRecords<Pack>().ToList();
            }

And Pack.cs

public class Pack {   
    public string NrDoc { get; set; }
    public string recipientName { get; set; }
    public string recipientAdress { get; set; }
    public string recipientCity { get; set; }
    public string packValue { get; set; }
    public string packInfo { get; set; }
     }

and PackMap.cs

 sealed class PackMap : ClassMap<Pack>
{
    public PackMap()
    {

        AutoMap();
        Map(m => m.NrDoc).Name("Numer Dokumentu");
        Map(m => m.recipientName).Name("Kontrahent");
        Map(m => m.recipientAdress).Name("Ulica");
        Map(m => m.recipientCity).Name("Miasto");
        Map(m => m.packValue).Name("Brutto");
        Map(m => m.packInfo).Name("Opis");
    }
}

In "PackMap.cs" I tried to use Index(0),Index(1), etc. and no changes. Who will tell me what I doing wrong? I must use different headers name in CSV file and different variables in C#

This is my csv file:

Numer Dokumentu;Status;Data wyst.;Magazyn;Kontrahent;Ulica;Miasto;Netto;Brutto;Opis
FA/3/08/2017/1;;16.08.2017;MAGAZYN;Damianowa Firma;Nowa Lucyna Herc;Lublin;87;20;107;25;Wystawić fakturę. Uwagi klienta:   1
FA/1/10/2017/6;;28.10.2017;MAGAZYN;IBIS Marek Jeż;Jana Pawła II;Szubin;241;00;296;43;Wysyłka
FA/2/10/2017/6;;28.10.2017;MAGAZYN;Netia  S.A.;ul. Poleczki 13;Warszawa;782;28;962;20;Wysyłka pobranie
like image 560
Damian Avatar asked Oct 30 '17 21:10

Damian


1 Answers

Your file is not comma separated, so you need to change the configuration to use ; instead of a comma.

reader.Configuration.Delimiter = ";";
like image 197
Josh Close Avatar answered Sep 30 '22 21:09

Josh Close