Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am trying to parse CSV file to a datatable using this code:
C#
//create the "database" connection string
      string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
        + "Data Source=\"" + dir + "\\\";"
        + "Extended Properties=\"text;HDR=Yes;FMT=Delimited\"";
      //create the database query
      string query = "SELECT * FROM " + file;
      //create a DataTable to hold the query results
      DataTable dTable = new DataTable();
      //create an OleDbDataAdapter to execute the query
      OleDbDataAdapter dAdapter =
         new OleDbDataAdapter(query, connString);
      try
      {
        //fill the DataTable
        dAdapter.Fill(dTable);
      }
      catch (InvalidOperationException /*e*/)
      { }


First line of the CSV file is defined as a header. There is a column named "Time Point 0.5(s)" in the header. When parsing it to the datatable, the column appeared as "Time Point 0#5(s)". The "." character becomes "#" character. This does not happen if the row is not header. How can I solve this problem?

Regards,
Posted

Have a look at this [^]blog. This is other way converting CSV into datatable. It might solve your problem. In addition to this you can try the this[^] CSV parser.

[EDITED]

This because, if you want try to bind your DataTable with such column it might throw exception due to (.) period character. For example if you try to bind the converted CSV records to DataBinder with Eval Method, it will throw exception. So to minimize such exception DataTable will convert the Column Name to other special character. See this[^] link for further.
 
Share this answer
 
v2
Hi,

Thank you for your reply. I did read those articles before, however, I would love to know the reason behind this problem, why "." character becomes "#" character as if it is in header row. While in the data row, it is fine.

Thanks,
 
Share this answer
 
Comments
Wonde Tadesse 6-Aug-11 22:14pm    
Answer updated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900