An item with the same key has already been added - csvreader.fieldcount
I'm trying to 开发者_如何转开发create an import program from CSV.
My code is
csv = New CsvReader(New StreamReader("CSVFileLocation"), True)
Dim fieldCount As Integer = csv.FieldCount
The error message "An item with the same key has already been added." on the second line. If I changed "HasReaders" to "False", there's no such error. But, I'm not able to get the Headers.
Could somebody help me on this, please?
FYI: I'm using Visual Studio 2010 version.
Regards, Richard
Check that your CSV file may have duplicate column names, or multiple empty cells, in the header row?
If that's the case, try to loop through your csv
object, and try rename the headers in code before calling the property FieldCount
.
My guess is that the CsvReader
class is going through the first row adding strings to a dictionary, and the header row has two cells with the same value (so two identically named fields). Take a look at your data and see if this is the case. Alternately, if you have access to the source code for CsvReader
, you could have it handle this case by naming the second field something slightly different (e.g., by appending a "1" onto the end of its name).
精彩评论