title: Npgsql 4.0 Parameters and Null Values
body: <p>Passing a null value using Npgsql looks something like this:</p>
  
  <pre><code>using (NpgsqlCommand cmd = new NpgsqlCommand("insert into foo values (:TEST)", conn))
  {
      cmd.Parameters.Add(new NpgsqlParameter("TEST", NpgsqlDbType.Varchar));
      cmd.Parameters[0].Value = DBNull.Value;
  
      cmd.ExecuteNonQuery();
  }
  </code></pre>
  
  <p>Which works fine.</p>
  
  <p>The new Npgsql 4.0 documentation recommends declaring parameters with a strong 
  datatype, like this:</p>
  
  <pre><code>using (NpgsqlCommand cmd = new NpgsqlCommand("insert into foo values (:TEST)", conn))
  {
      cmd.Parameters.Add(new NpgsqlParameter&lt;string&gt;("TEST", NpgsqlDbType.Varchar));
      cmd.Parameters[0].Value = DBNull.Value;
  
      cmd.ExecuteNonQuery();
  }
  </code></pre>
  
  <p>When the DBNull.Value is passed, a general exception is thrown:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'System.DBNull' to type 'System.String'.</p>
  </blockquote>
  
  <p>Everything still works with the new boxing-less parameters, but the new syntax seems to make sense, and we want to use it...  but how to resolve this datatype disconnect?</p>
  
  <p>The example above is with a string.  I assume this will also impact numerics and dates.</p>