I'm using the CSVHelper NuGet package and am getting the error "Fields do not exist in CSV file." Here is my code:
using (TextReader prodFile = System.IO.File.OpenText(filePath))
{
CsvReader csv = new CsvReader(prodFile);
List<PulProduct> prodList = csv.GetRecords<PulProduct>().ToList();
}
class
public class PulProduct
{
public string PartNumber { get; set; }
public string PPartNumber { get; set; }
public string VPartNumber { get; set; }
public string VPPartNumber { get; set; }
public string Status { get; set; }
public string Description { get; set; }
public decimal ORetail { get; set; }
public decimal CSRetail { get; set; }
public decimal BDPrice { get; set; }
public decimal YDPrice { get; set; }
public string Hazardous { get; set; }
public string TruckPart { get; set; }
public string PartAddDate { get; set; }
public int AvailabilityWI { get; set; }
public int AvailabilityNY { get; set; }
public int AvailabilityTX { get; set; }
public int AvailabilityCA { get; set; }
public int AvailabilityNV { get; set; }
public int AvailabilityNC { get; set; }
public int AvailabilityNational { get; set; }
public string Trademark { get; set; }
public string AdPolicy { get; set; }
public string PriceChanged { get; set; }
public string UOM { get; set; }
public string UPC { get; set; }
public string BrandName { get; set; }
public string Country { get; set; }
public string Weight { get; set; }
public string Closeout { get; set;}
public string NoShipToCA { get; set; }
public string Notes {get; set; }
}
CSVHelper documentation says that CSVHelper will automatically map my class to the CSV file. I'm not sure what I am doing wrong.
The full exception is:
An exception of type 'CsvHelper.CsvMissingFieldException' occurred in CsvHelper.dll but was not handled in user code
Additional information: Fields 'PartNumber' do not exist in the CSV file.
Here is a sample header and first line:
Part Number,Punctuated Part Number,Vendor Part Number,Vendor Punctuated Part Number,Part Status,Part Description,Original Retail,Current Suggested Retail,Base Dealer Price,Your Dealer Price,Hazardous Code,Truck Part Only,Part Add Date,WI Availability,NY Availability,TX Availability,CA Availability,NV Availability,NC Availability,National Availability,Trademark,Ad Policy,Price Changed Today,Unit of Measure,UPC Code,Brand Name,Country of Origin,Weight,Closeout Catalog Indicator,NoShipToCA, Notes
0023451,001-0901,0067401,067-0401,S,4-1 SYS OBR CB350/4,399.95,352.95,384.40,214.40,,,19341102,0,0,0,0,0,0,0,,,N,EA,879345348000086,MAC,US,13.80,N, ,
One of the most common CSV import errors is that the file is simply too large. That can be caused by too many fields or records in the file, too many columns, or too many rows. The import error can be caused by limits set by the program using the file or the amount of available memory on the system.
We can read csv file by two ways : 1 Read data line by line : Lets see how to read CSV file line by line. For reading data line by line, first we have to... 2 Read all data at once : We read the CSV records one by one using the readNext () method. CSVReader also provides a... More ...
CSVReader – This class provides the operations to read the CSV file as a list of String array. CSVWriter – This class allows us to write the data to a CSV file. CsvToBean – This class will be used when you want to populate your java beans from a CSV file content. BeanToCsv – This class helps to export data to CSV file from your java application.
When we need to skip the first element in the list then we can specify start line while creating CSVReader. CSV files can be separated with a delimiter other than a comma e.g. semi-colon, pipe etc.
Your field names and your file column headers do not match due to whitespace. In PulProduct, the first field is "PartNumber". In your sample file, the first column us "Part Number". Setting IgnoreHeaderWhiteSpace to true in the CsvConfiguration object will handle this.
using (TextReader prodFile = System.IO.File.OpenText(filePath))
{
CsvReader csv = new CsvReader(prodFile);
csv.Configuration.IgnoreHeaderWhiteSpace = true;
List<PulProduct> prodList = csv.GetRecords<PulProduct>().ToList();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With