CsvReader skipping first (non header) record row when HasHeaderRecord set true. #2204
-
Hi all, not sure if this should be raised as a bug or if I am just doing something wrong with my setup for CsvReader. The last entry I could find similar to this dates back to 2015. In summary, CsvReader/CsvDataReader appears to be skipping the second row of data when HasHeaderRecord is set to true and I have ShouldSkipRecord configured; after the header record is skipped, the second row of data gets treated as the header record for all subsequent rows of data and subsequently skipped despite returning false for a ShouldSkipRecord check, For a more verbose description; I am using CsvReader in conjunction with CsvDataReader and SqlBulkCopy, and when I have a CsvReader set with HasHeaderRecord and I also have ShouldSkipRecord configured I am seeing the following behaviour:
If I disable HasHeaderRecord, everything works but that does not sit right and I have to ask if the above is by design, why? As noted in the below code, if I remove the HasHeaderRecord check from the skip record lambda method and set the HasHeaderRecord to false at configuration level, the below code works. The above is using the latest public package, so 30.0.1 targeting ,net 6.0 The code in questions looks as follows:
with the ValidateMyColumn method above being a static extension method looking something like:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I think you have your logic off a little bit. !arg.Row.ValidateMyColumn(objectForLogRef) along with if (row.Configuration.HasHeaderRecord && row.Parser.Row == 1)
{
return false;
} will return You don't want it to skip the header. That means it will skip the first row and end up reading the next row as the header row. Your code should be if (row.Configuration.HasHeaderRecord && row.Parser.Row == 1)
{
return true;
} |
Beta Was this translation helpful? Give feedback.
The issue is not the ! operator. The issue is your code is telling
CsvHelper
to skip reading the first row, which has your header. Switching all the true/false responses along with the ! operator will still end up with the same result.You need to return
false
toShouldSkipRecord
if it is the header row. You also need to returnfalse
if the record is valid. Only returntrue
toShouldSkipRecord
if the record is not valid.With your original code I get a
HeaderValidationException
because the code is tellingCsvReader
to skip the first row if you have a header.