title: Handling DBNull values for DateTime
body: <p>I'm converting my <code>DataTable</code> to generic List and as I'm converting row value to object for the list, it's giving Cast Exception. I've tried handling the DBNull using the code</p>
  
  <pre><code>if (dataRow["DateCompleted"] == DBNull.Value)
  {
      dataRow["DateCompleted"] = null;
  }
  if (dataRow["TotalRecords"] == DBNull.Value)
  {
      dataRow["TotalRecords"] = 0;
  }
  if (dataRow["CompanyName"] == DBNull.Value)
  {
      dataRow["CompanyName"] = "";
  }
  </code></pre>
  
  <p>but DateCompleted is not accepting the null, DBNull or empty string.</p>
  
  <p>After the process, I'm making object like </p>
  
  <pre><code>DemoData demoValue = new DemoData
  {
      CompanyName = dataRow["CompanyName"].ToString(),
      DateCompleted = (DateTime)dataRow["DateCompleted"],
      TotalRecords = (int)dataRow["TotalRecords"]
  };
  </code></pre>
  
  <p>and adding the object to the list</p>
  
  <pre><code>DataList.Add(demoValue);
  </code></pre>
  
  <p>My list is </p>
  
  <pre><code>public List&lt;DemoData&gt; DataList = new List&lt;DemoData&gt;();
  </code></pre>
  
  <p>and my class is</p>
  
  <pre><code>public class DemoData
  {
      public string CompanyName { get; set; }
      public DateTime DateCompleted { get; set; }
      public int TotalRecords { get; set; }
  }
  </code></pre>