CSV headers not loading

33 views
Skip to first unread message

Chris Corbett

unread,
Jun 27, 2017, 10:24:38 AM6/27/17
to CSVChat, ccor...@busapps.com.au
Hi there, we testing CSV reader and can load most CSV files without any issue.

When attempting to load a particular CSV file which is genereated by a client application export, the headers are not read. The column names appear as Column1, Column2 etc.

Reading through the posts the two options I could see as possible solutions were to ensure no duplicate column names and ensure the read header option is set to true.
Both have been attempted without success. Could you please assist with why this may be occuring?

shriop

unread,
Jun 27, 2017, 10:40:04 AM6/27/17
to CSVChat, ccor...@busapps.com.au
The ReadToEnd method of CsvReader generally only does that when duplicate column names are found. Since you've checked for duplicates already, my guess is that there are empty header names on the end that are being recognized as duplicates or something odd like that.

To troubleshoot, instead of calling ReadToEnd, call ReadHeaders and then see what values are returned in the Headers property. You can also look at the headers by viewing the file in Notepad and see if the header line has noticeable issues.

I'm pasting the logic of ReadToEnd below to help you troubleshoot. It's really just a simple convenience method, but you'll see that it gets a little verbose in attempting to prevent exceptions when naming the columns.

If you can reply by pasting what the header line looks like in Notepad, maybe I can help identify the issue, or you can email me a sample file.

Bruce Dunwiddie

public DataTable ReadToEnd(bool readHeaders, ulong maxRecords)
{
DataTable csvTable = new DataTable();

// turn off notifications and constraints for faster loading
csvTable.BeginLoadData();

if (readHeaders)
{
ReadHeaders();

bool preserveNames = true;

for (int i = 0; i < headersHolder.Length; i++)
{
if (preserveNames)
{
string header = headersHolder.Headers[i];

if (header.Length == 0)
{
header = "Column" + (i + 1);
}

// check for duplicates
if (csvTable.Columns.Contains(header))
{
// change the naming convention to prevent exceptions
for (int j = i - 1; j >= 0; j--)
{
csvTable.Columns[j].ColumnName = "Column" + (j + 1);
}

preserveNames = false;
}
else
{
// use the name from the data
csvTable.Columns.Add(header);
}
}

if (!preserveNames)
{
// use a name based on the column index
csvTable.Columns.Add("Column" + (i + 1));
}
}
}

int currentColumnCount = headersHolder.Length;

bool partialReading = maxRecords > 0;

DataRowCollection rows = csvTable.Rows;

while (
(
!partialReading ||
currentRecord < maxRecords
) &&
ReadRecord())
{
if (columnsCount > currentColumnCount)
{
// add extra columns because we encountered a wider row
// or this is the first row and readHeaders == false
for (int i = currentColumnCount; i < columnsCount; i++)
{
csvTable.Columns.Add("Column" + (i + 1));

for (int currentRows = 0; currentRows < (int) currentRecord - 1; currentRows++)
{
rows[currentRows][i] = "";
}
}

currentColumnCount = columnsCount;
}

rows.Add(Values);

for (int columnsNeeded = columnsCount; columnsNeeded < currentColumnCount; columnsNeeded++)
{
rows[rows.Count - 1][columnsNeeded] = "";
}
}

// reenable notifications and constraints
csvTable.EndLoadData();

return csvTable;

shriop

unread,
Jun 28, 2017, 8:17:44 AM6/28/17
to CSVChat, ccor...@busapps.com.au
After looking at your sample file, it looks like "Basic TPD Amount" is duplicated in the headers. That's the only one and if I change it's name, the other column names come out fine.

Bruce Dunwiddie
Reply all
Reply to author
Forward
0 new messages