Based on the instructions in Getting Started with CsvHelper, I have the following code except for the csv.Configuration.BadDataFound line:
using (var csv = new CsvReader(tr, CultureInfo.InvariantCulture))
{
List<string> badPeople = new List<string>();
csv.Configuration.BadDataFound = context => badPeople.Add(context.RawRecord);
try
{
var people = csv.GetRecords<Person>();
var count = people.Count();
response = await Http.PostAsJsonAsync("api/people/batch", people);
Message = $"{response}: {selectedFile.Count} file uploaded";
}
catch (Exception ex)
{
Message = ex.Message;
}
}
I configured the BadDataFound line based on suggestions from the following:
- Handling bad CSV records in CsvHelper
- CSVHelper: logging errors when BadDataException is thrown
- CSVHelper BadDataFound in a valid csv and numerous others.
In the CsvHelper.Configuration documentation it states:
Gets or sets the function that is called when bad field data is found. A field has bad data if it contains a quote and the field is not quoted (escaped). You can supply your own function to do other things like logging the issue instead of throwing an exception. Arguments: context
I keep getting the error that BadDataFound does not have a setter, and based on what I saw when looking at the code on GitHub, that makes sense. The error message I get when I run without the BadDataFOund line states that one can ignore the BadDataFound by nulling it.
To complicate matters, there is also no "RawRecord" in the context that I can find.
How do I get this to work?