title: Cannot cast DBNull.Value to type System.DateTime
body: <p>I have had to pull two separate queries of data from separate databases and now I am trying to merge then into one datatable in C#. But I am getting an error whenever I have null values coming from a column containing mostly datetime values. I just can't seem to find out what I can do to fix it. i have tried if statements to catch the null values etc but keep getting exceptions. </p>
  
  <p>I am getting the following error:</p>
  
  <pre><code>System.InvalidCastException was unhandled
  HResult=-2147467262
    Message=Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type.
    Source=System.Data.DataSetExtensions
    StackTrace:
         at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
         at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
         at HueniData.Program.Main(String[] args) in c:\Visual Studio Projects\HueniData\HueniData\Program.cs:line 60
         at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
         at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
         at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
    InnerException: 
  </code></pre>
  
  <p>for the following code:</p>
  
  <pre><code>var resultTable = from t1 in actualTable.AsEnumerable()
                    join t2 in recipeTable.AsEnumerable()
                      on t1.Field&lt;int&gt;("Step No") equals t2.Field&lt;int&gt;("Step No")
                    select new { t1, t2 };
  
  
  DataTable newTable = new DataTable();
  newTable.Columns.Add("StepNo", typeof(int));
  newTable.Columns.Add("ActualDuration", typeof(decimal));
  newTable.Columns.Add("RecipeDuration", typeof(decimal));
  newTable.Columns.Add("StartTime", typeof(DateTime));
  
  
  DataRow newRow;
  foreach (var dr in resultTable)
  {
      newRow = newTable.NewRow();
      newRow["StepNo"] = dr.t1.Field&lt;int&gt;("Step No");
      newRow["ActualDuration"] = dr.t1.Field&lt;decimal&gt;("ActualDuration");
      newRow["RecipeDuration"] = dr.t2.Field&lt;decimal&gt;("RecipeDuration");
      **newRow["StartTime"] = dr.t1.Field&lt;DateTime&gt;("Step Start Time");**                                           
      newTable.Rows.Add(newRow);
  }
  </code></pre>
  
  <p>UPDATE:
  The below answers were helpful but I have basically attempted to do both those originally using various if statements and changing the datetime to datetime?. I still get the error. The way I resolved this was by using:</p>
  
  <pre><code> newTable.Columns.Add("StartTime", typeof(Object));
  newRow["StartTime"] = dr.t1.Field&lt;Object&gt;("Step Start Time");
  </code></pre>
  
  <p>But if anyone can explain to me what the problem was I would still love to know. </p>