title: Passing DBNull.Value and Empty textbox value to database
body: <p>I have some textboxes on my page which can be empty because they are optional and I have this DAL code</p>
  
  <pre><code>parameters.Add(new SqlParameter("@FirstName", FirstName));
  parameters.Add(new SqlParameter("@LastName", LastName));
  parameters.Add(new SqlParameter("@DisplayName", DisplayName));
  parameters.Add(new SqlParameter("@BirthDate", BirthDate));
  parameters.Add(new SqlParameter("@Gender", Gender));
  </code></pre>
  
  <p>Any of those fields can be empty. The problem is when they are empty I receive <code>Procedure XXX requires @FirstName which was not supplied</code></p>
  
  <p>Then I changed my code to</p>
  
  <pre><code>parameters.Add(new SqlParameter("@FirstName", String.IsNullOrEmpty(FirstName) ? DBNull.Value : (object)FirstName));
  parameters.Add(new SqlParameter("@LastName", String.IsNullOrEmpty(LastName) ? DBNull.Value : (object) LastName));
  parameters.Add(new SqlParameter("@DisplayName", String.IsNullOrEmpty(DisplayName) ? DBNull.Value : (object) DisplayName));
  parameters.Add(new SqlParameter("@BirthDate", BirthDate.HasValue ? (object)BirthDate.Value : DBNull.Value));
  parameters.Add(new SqlParameter("@Gender", String.IsNullOrEmpty(Gender) ? DBNull.Value : (object) Gender));
  </code></pre>
  
  <p>But this looks messy to me especially the casting to <code>object</code> because ternary statement requires both value to be the same type.</p>
  
  <p>Why is empty string or null string not treated <code>NULL</code> in the database? If I have to convert this to <code>DBNull.Value</code> is there a cleaner way? Saving the value as empty string in the database could have helped but query for <code>NULL</code> in the database will get messy too</p>
  
  <p>Please give your advice on common practices or something close to that.</p>