title: How to add CsvHelper records to DataTable to use for SqlBulkCopy to the database
body: <p>I am trying to read a CSV file with CsvHelper, load each record into a DataTable, and then use SqlBulkCopy to insert the data into a database table. With the current code, I get an exception when adding a row to the DataTable. The exception is: "Unable to cast object of type 'MvcStockAnalysis.Models.StockPrice' to type 'System.IConvertible'.Couldn't store  in Date Column.  Expected type is DateTime."</p>
  
  <p>The example CSV file is from yahoo finance. For example: <a href="http://ichart.yahoo.com/table.csv?s=MMM&amp;a=0&amp;b=1&amp;c=2010&amp;d=0&amp;e=17&amp;f=2014&amp;g=d&amp;ignore=.csv" rel="noreferrer">http://ichart.yahoo.com/table.csv?s=MMM&amp;a=0&amp;b=1&amp;c=2010&amp;d=0&amp;e=17&amp;f=2014&amp;g=d&amp;ignore=.csv</a></p>
  
  <p>The CSV file contains the following header:
   Date   Open    High    Low Close   Volume  Adj Close</p>
  
  <p>The model that I am reading the CSV file into:</p>
  
  <pre><code>namespace MvcStockAnalysis.Models
  {
      using System;
      using System.Collections.Generic;
  
      public partial class StockPrice
      {
          public int Id { get; set; }
          public System.DateTime Date { get; set; }
          public int CompanyId { get; set; }
          public double High { get; set; }
          public double Low { get; set; }
          public double Close { get; set; }
          public double AdjClose { get; set; }
          public double Open { get; set; }
          public double Volume { get; set; }
  
          public virtual Company Company { get; set; }
      }
  }
  </code></pre>
  
  <p>The mapping of the CSV file to the StockPrice class uses the following:</p>
  
  <pre><code>public class StockPriceClassMap : CsvClassMap&lt;StockPrice&gt;
  {
      public override void CreateMap()
      {
          Map(m =&gt; m.Date).Name("Date");
          Map(m =&gt; m.Close).Name("Close");
          Map(m =&gt; m.AdjClose).Name("Adj Close");
          Map(m =&gt; m.High).Name("High");
          Map(m =&gt; m.Low).Name("Low");
          Map(m =&gt; m.Open).Name("Open");
          Map(m =&gt; m.Volume).Name("Volume");
      }
  }
  </code></pre>
  
  <p>The code that tries to add the CsvHelper records to the DataTable is as follows:</p>
  
  <pre><code>var connectionstring = ConfigurationManager.ConnectionStrings["MvcStockAnalysis.Models.MvcStockAnalysisContext"];
  var connection = new SqlConnection();
  connection.ConnectionString = connectionstring.ToString();
  var destinationTableName = "StockPrices";
  var company = db.Company
              .Where(c =&gt; c.Symbol == "MMM")
              .FirstOrDefault();
  
  try
  {
      string path = HttpContext.Server.MapPath("~/App_Data/" + company.Symbol + @".csv");
  
      if (System.IO.File.Exists(path))
      {     
  
          using (StreamReader sr = new StreamReader(path))
          {
              using (var csv = new CsvReader(sr))
              {
                  DataTable dt = new DataTable("StockPrices");
                  csv.Configuration.HasHeaderRecord = true;
                  csv.Configuration.RegisterClassMap&lt;StockPriceClassMap&gt;();
  
                  dt.Columns.Add(new DataColumn("Date", typeof(DateTime)));
                  dt.Columns.Add(new DataColumn("Close", typeof(Double)));
                  dt.Columns.Add(new DataColumn("AdjClose", typeof(Double)));
                  dt.Columns.Add(new DataColumn("High", typeof(Double)));
                  dt.Columns.Add(new DataColumn("Low", typeof(Double)));
                  dt.Columns.Add(new DataColumn("Open", typeof(Double)));
                  dt.Columns.Add(new DataColumn("Volume", typeof(Double)));
                  dt.Columns.Add(new DataColumn("CompanyId", typeof(Double)));
                  var records = csv.GetRecords&lt;StockPrice&gt;().ToList();
                  foreach (var record in records)
                  {                                    
                      record.CompanyId = company.Id;
                      dt.Rows.Add(record);
                  }
                  // add dt to the database
                  using (var bulkCopy = new SqlBulkCopy(connection.ConnectionString))
                  {
                      // DataTable column names match my SQL Column names, so I simply made this loop. 
                      foreach (DataColumn col in dt.Columns)
                      {
                          bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
                      }
                      bulkCopy.DestinationTableName = destinationTableName;
                      bulkCopy.WriteToServer(dt);
                  }
              }
          }
      }
      connection.Close();
  }
  catch (Exception e)
  {
      Console.Write(e.Message);
  }
  </code></pre>
  
  <p>How can I add CsvHelper records to DataTable to use for SqlBulkCopy to the database?</p>