0

I'm reading in a csv file from a datatable and something is happening.

Here's the read

 OpenFileDialog openFileDialog1 = new OpenFileDialog();
 openFileDialog1.Filter = "CSV Files|*.csv";
 openFileDialog1.Title = "Select a CSV File";
 if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {

            string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;", System.IO.Path.GetDirectoryName(openFileDialog1.FileName));
            string cmdString = string.Format("SELECT * FROM {0}", System.IO.Path.GetFileName(openFileDialog1.FileName));
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdString, connString);
            DataTable tbl = new DataTable();
            dataAdapter.Fill(tbl);
            TableBuilder b = new TableBuilder(tbl);
            List<SingleTable> tablelist = b.TableList;
        }

And when I look at the datatable in the visualizer, it looks like ![The first row, instead of being a row, is a column header][1]

The original csv looks like this

![original csv ][2]

Can someone tell me why the datatable is converting the first row into a column? And what should I do to preserve that first row?

Jeffrey Easley
  • 306
  • 2
  • 6
  • 17

3 Answers3

1

Try to tell the OLEDB-Provider that the file has no header

Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;HDR=no;

Or have a look at this SO-Answer: https://stackoverflow.com/a/4598862/284240

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Not sure if this is what you need cause it seems that something is missing in your question. But you can change your connection string to:

string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;HDR=No", System.IO.Path.GetDirectoryName(openFileDialog1.FileName));

This will tell the connection that the first line is not a header row.

gabsferreira
  • 3,089
  • 7
  • 37
  • 61
0

Normally if you have extra ","'s embedded in the data, this will treat every comma as a separate column I had this happen when I was doing the samething.. I had to check the column data which caused the field offset.. also are there Headers that you are creating..?

MethodMan
  • 18,625
  • 6
  • 34
  • 52