title: SqlParameterCollection only accepts non-null SqlParameter type objects, not DBNull objects
body: <p>When I add the SQL parameter <code>p</code> to the collection I get an <code>InvalidCastException</code> with the message from the post title.</p>
  
  <p><code>parentId</code> is a nullable integer and a nullable integer in the database.</p>
  
  <p>Why do I get this exception and how can I solve it?</p>
  
  <p>I do not use stored procedures and I have read the similar threads but they did not help me.</p>
  
  <pre><code>var p = new SqlParameter("ParentId", SqlDbType.Int).Value = parentId ?? (object) DBNull.Value;
  cmd.Parameters.Add(p);  
  </code></pre>