0

I have a .csv file, with header column in first row. I have issue when read values from this file, although I follow sample code in CsvHelper main page (https://joshclose.github.io/CsvHelper/getting-started/).

My issue: 'There is no header record to determine the index by name.'

There're paths of my code:

Controller:

      public void TestCSV()
      {
         try
         {
            using (var reader = new StreamReader(Server.MapPath("/Resource/DHLProject/DHLAttachedFiles/HANG NHAP.csv")))
            {
               string line = null;
               while ((line = reader.ReadLine()) != null)
               {
                  Console.WriteLine(line);
                  using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                  {
                     csv.Configuration.HasHeaderRecord = false;
                     csv.Configuration.HeaderValidated = null;
                     csv.Configuration.Delimiter = ",";
                     csv.Configuration.RegisterClassMap<DHL_ImportModelMap>();
                     csv.Read();
                     var record = csv.GetRecord<DHL_ImportModel>();
                  }
               }
            }
         }
         catch (Exception ex)
         {
         }
      }

Model:

   public class DHL_ImportModelMap : ClassMap<DHL_ImportModel>
   {
      public DHL_ImportModelMap()
      {
         Map(m => m.BillingAccount).Name("Billing Account");
         Map(m => m.BillingAccountName).Name("Billing Account Name");
         Map(m => m.VATNumber).Name("VAT Number");
         Map(m => m.ShipmentNumber).Name("Shipment Number");
         Map(m => m.ShipmentDate).Name("Shipment Date");
         Map(m => m.ProductName).Name("Product Name");
         Map(m => m.OriginalCountryCode).Name("Orig Country Code");
         Map(m => m.OriginalCountryName).Name("Orig Country Name");
         Map(m => m.Sender).Name("Senders Name");
         Map(m => m.DestinationCode).Name("Destination");
         Map(m => m.DestinationName).Name("Dest Name");
         Map(m => m.DestinationCountryCode).Name("Dest Country Code");
         Map(m => m.DestinationCountryName).Name("Dest Country Name");
         Map(m => m.ReceiversCompanyName).Name("Receivers Name");
         Map(m => m.ReceiversCompanyAddress1).Name("Receivers Address 1");
         Map(m => m.ReceiversCompanyAddress2).Name("Receivers Address 2");
         Map(m => m.ReceiversContact).Name("Receivers Contact");
         Map(m => m.Weight).Name("Weight (kg)");
         Map(m => m.Currency).Name("Currency");
         Map(m => m.Total_amount_non_VAT).Name("Total amount (excl. VAT)");
         Map(m => m.Total_amount_include_VAT).Name("Total amount (incl. VAT)");
      }
   }
   public class DHL_ImportModel
   {
      [Name("Billing Account")]
      public string BillingAccount { get; set; }
      [Name("Billing Account Name")]
      public string BillingAccountName { get; set; }
      [Name("VAT Number")]
      public string VATNumber { get; set; }
      [Name("Shipment Number")]
      public string ShipmentNumber { get; set; }
      [Name("Shipment Date")]
      public string ShipmentDate { get; set; }
      [Name("Product Name")]
      public string ProductName { get; set; }
      [Name("Orig Country Code")]
      public string OriginalCountryCode { get; set; }
      [Name("Orig Country Name")]
      public string OriginalCountryName { get; set; }
      [Name("Senders Name")]
      public string Sender { get; set; }
      [Name("Destination")]
      public string DestinationCode { get; set; }
      [Name("Dest Name")]
      public string DestinationName { get; set; }
      [Name("Dest Country Code")]
      public string DestinationCountryCode { get; set; }
      [Name("Dest Country Name")]
      public string DestinationCountryName { get; set; }
      [Name("Receivers Name")]
      public string ReceiversCompanyName { get; set; }
      [Name("Receivers Address 1")]
      public string ReceiversCompanyAddress1 { get; set; }
      [Name("Receivers Address 2")]
      public string ReceiversCompanyAddress2 { get; set; }
      [Name("Receivers Contact")]
      public string ReceiversContact { get; set; }
      [Name("Weight (kg)")]
      public string Weight { get; set; }
      [Name("Currency")]
      public string Currency { get; set; }
      [Name("Total amount (excl. VAT)")]
      public string Total_amount_non_VAT { get; set; }
      [Name("Total amount (incl. VAT)")]
      public string Total_amount_include_VAT { get; set; }

   }

1 Answer 1

2

If your file has a header record then you should remove

csv.Configuration.HasHeaderRecord = false;  --Remove this line

This should only be used if your file does not have a header record in the first row.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.