title: Dealing with DBNull.Value
body: <p>I frequently have to deal with DataTables connected to grid controls, custom updating always seems to produce a lot of code related to DBNull.Value. I saw a similar question here but think there must be a better answer:</p>
  
  <p><a href="https://stackoverflow.com/questions/26809/what-is-the-best-way-to-deal-with-dbnulls">What is the best way to deal with DBNull&#39;s</a></p>
  
  <p>The thing I find is I tend to encapsulate my database updates in methods so I end up with code like below where I move the DBNull.value to a nullable type and then back for the update:</p>
  
  <pre><code>private void UpdateRowEventHandler(object sender, EventArgs e)
  {
      Boolean? requiresSupport = null;
      if (grdMainLevel1.GetFocusedRowCellValue(colASRequiresSupport) != DBNull.Value)
          requiresSupport = (bool)grdMainLevel1.GetFocusedRowCellValue(colASRequiresSupport);
  
      AdditionalSupport.UpdateASRecord(year, studentID, requiresSupport)
  }
  
  internal static void UpdateASRecord(
          string year,
          string studentID,            
          bool? requiresSupport)
      {
          List&lt;SqlParameter&gt; parameters = new List&lt;SqlParameter&gt;();
  
          parameters.Add(new SqlParameter("@year", SqlDbType.Char, 4) { Value = year });
          parameters.Add(new SqlParameter("@student_id", SqlDbType.Char, 11) { Value = studentID });
  
          if (requiresSupport == null)
              parameters.Add(new SqlParameter("@requires_support", SqlDbType.Bit) { Value = DBNull.Value });
          else
              parameters.Add(new SqlParameter("@requires_support", SqlDbType.Bit) { Value = requiresSupport });
  
          //execute sql query here to do update
      }
  </code></pre>
  
  <p>That was just an example of the flow and not working code. I realize I could do things like pass objects or swallow potential casting problems using "as type" to get DBUll straight to null but both of these to me appear to hide potential errors, I like the type safety of the method with nullable types.</p>
  
  <p>Is there a cleaner method to do this while maintaining type safety?</p>