Allowing for no headers and default column names

Sep 5, 2009 at 5:21 AM

I'm wondering if it's possible to allow the app to work without headers being supplied.  Some files don't have headers but the data is all there.

Also I get an error that says that there are duplicate columns when the first line has more than one blacnk value since it's trying to use this as the header.  In the case of a black column it would be usefull to just anme it column and then the index number of hte columns (ie. Column 4).

 

Otherwise you have a tremendously good application here.  I'll promote it anytime I hear about someone trying to import CSVs from now on.

Coordinator
Sep 7, 2009 at 11:14 AM

Thanks for the kind words.

KBCsv does support your scenario. You just need to ensure that you do not call the ReadHeaderRecord method. For example:

class Program
{
    static void Main(string[] args)
    {
        using (var reader = new CsvReader("data.csv"))
        {
            //don't call this if you don't have a header record
            //reader.ReadHeaderRecord();

            foreach (var record in reader.DataRecords)
            {
                foreach (var value in record.Values)
                {
                    Console.Write(value);
                    Console.Write(" ");
                }

                Console.WriteLine();
            }
        }

        Console.ReadKey();
    }
}

Best,
Kent

Jun 24, 2010 at 7:00 AM

Hi,

I am considering using KBCSV as standard data providers seem to have issues with things like leading zeros, white space etc.

I am wanting to write code that reads a CSV file into and unbound data set particularly with leading zeros intact.

I assume KBCsv handles date strings OK such as 24/06/2010.

If the CSV has no header row then I want to put it into a DataSet that then has columns named C1, C2, C3 etc.

My current code achieves this by updating the column names of the DataSet after it has been filled with data.

I noticed in the documentation for KBCSV where it refers to the Fill method it says:

"The header record for the CsvReader must be set prior to invoking this method. The data read must conform to the header record. That is, if a record is found with more columns than specified by the header record, an exception will be thrown."

Does this mean that in order to use the Fill method you have to call ReadHeaderRecord or does this mean that if you call ReadHeaderRecord it must be before fill?

Can I use the fill method without a header row in the CSV?

In other words: How would I fill a dataset from a csv that has no header record but set the column headings of the data table to C1,C2,C3 etc.

Thanks,

Tim

 

Jun 28, 2010 at 2:20 AM

After some experimentation I found that the Fill function throws an exception if you don't have a header record.  So I wrote a method in my code to create a dummy header for a headerless CSV.

Eg

 

if (header)

fill data set

else

read first record to get column count

create dummy header

fill data set.

Problem solved!

Tim