tags:

views:

27

answers:

2

Hi,

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

A: 

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.

p.campbell
Thanks a lot for your help. Sorry for my belated reply as I was on leave. Now, I could resolve the issue. It's because of the "Multiple Empty Cells" in CSV file. Regards, Richard
Richard
A: 

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

Dan Tao