title: Powershell and SQL parameters. If empty string, pass DBNull
body: <p>I got this parameter:</p>
  
  <pre><code>$objDbCmd.Parameters.Add("@telephone", [System.Data.SqlDbType]::VarChar, 18) | Out-Null;
  $objDbCmd.Parameters["@telephone"].Value = $objUser.Telephone;
  </code></pre>
  
  <p>Where the string <code>$objUser.Telephone</code> can be empty. If it's empty, how can I convert it to <code>[DBNull]::Value</code>?</p>
  
  <p>I tried:</p>
  
  <pre><code>if ([string]:IsNullOrEmpty($objUser.Telephone)) { $objUser.Telephone = [DBNull]::Value };
  </code></pre>
  
  <p>But that gives me the error:</p>
  
  <blockquote>
    <p>Exception calling "ExecuteNonQuery" with "0" argument(s): "Failed to convert parameter value from a ResultPropertyValueCollection to a String."</p>
  </blockquote>
  
  <p>And if I convert it to a string, it inserts an empty string <code>""</code>, and not <code>DBNull</code>.</p>
  
  <p>How can this be accomplished?</p>
  
  <p>Thanks.</p>