title: Inserting null values into date fields?
body: <p>I have a FormView where I pull data from one table (MS Access), and then insert it (plus more data) into another table. I'm having issues with the dates. </p>
  
  <p>The first table has two date fields: <code>date_submitted</code> and <code>date_updated</code>. In some records, <code>date_updated</code> is blank. This causes me to get a data mismatch error when attempting to insert into the second table.</p>
  
  <p>It might be because I'm databinding the <code>date_updated</code> field from the first table into a HiddenField on the FormView. It then takes the value from the HiddenField and attempts to insert it into the second table:</p>
  
  <pre><code>Dim hfDateRequestUpdated As HiddenField = FormView1.FindControl("hfDateRequestUpdated")
  myDateRequestUpdated = hfDateRequestUpdated.Value
  '... It then attempts to insert myDateRequestUpdated into the database.
  </code></pre>
  
  <p>It works when there is a value there, but apparently you can't insert nothing into a date/time field in Access. I suppose I could make a second insert statement that does not insert into <code>date_updated</code> (to use when there is no value in<code>date_updated</code>), but is that the only way to do it? Seems like there should be an easier/less redundant way.</p>
  
  <p><strong>EDIT</strong>: </p>
  
  <p>Okay. So I've tried inserting SqlDateTime.Null, Nothing, and DBNull.Value. SqlDateTime.Null results in the value 1/1/1900 being inserted into the database. "Nothing" causes it to insert 1/1/2001. And if I try to use DBNull.Value, it tells me that it cannot be converted to a string, so maybe I didn't do something quite right there. At any rate, I was hoping that if there was nothing to insert that the field in Access would remain blank, but it seems that it has to fill it with something...</p>
  
  <p><strong>EDIT</strong>: </p>
  
  <p>I got DBNull.Value to work, and it does insert a completely blank value. So this is my final working code:</p>
  
  <pre><code>Dim hfDateRequestUpdated As HiddenField = FormView1.FindControl("hfDateRequestUpdated")
  Dim myDateRequestUpdated = Nothing
  
  If hfDateRequestUpdated.Value = Nothing Then
      myDateRequestUpdated = DBNull.Value
  Else
      myDateRequestUpdated = DateTime.Parse(hfDateRequestUpdated.Value)
  End If
  </code></pre>
  
  <p>Thanks everyone!</p>