Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An item with the same key has already been added - csvreader.fieldcount

Tags:

csv

vb.net

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

like image 803
Richard Avatar asked Sep 08 '10 01:09

Richard


2 Answers

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.

like image 170
p.campbell Avatar answered Nov 09 '22 20:11

p.campbell


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).

like image 21
Dan Tao Avatar answered Nov 09 '22 20:11

Dan Tao