title: Exception when AddWithValue parameter is NULL
body: <p>I have following code for specifying parameters for SQL query. I am getting following exception when I use <code>Code 1</code>; but works fine when I use <code>Code 2</code>. In <code>Code 2</code> we have a check for null and hence a <code>if..else</code> block. </p>
  
  <p>Exception:  </p>
  
  <blockquote>
    <p><em>The parameterized query '(@application_ex_id nvarchar(4000))SELECT  E.application_ex_id A' expects the parameter '@application_ex_id', which was not supplied.</em></p>
  </blockquote>
  
  <p><strong>Code 1</strong>:</p>
  
  <pre><code>command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID);
  </code></pre>
  
  <p><strong>Code 2</strong>:</p>
  
  <pre><code>if (logSearch.LogID != null)
  {
           command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID);
  }
  else
  {
          command.Parameters.AddWithValue("@application_ex_id", DBNull.Value );
  }
  </code></pre>
  
  <p><strong>QUESTION</strong></p>
  
  <ol>
  <li><p>Can you please explain why it is unable to take NULL from logSearch.LogID value in Code 1 (but able to accept DBNull)? </p></li>
  <li><p>Is there a better code to handle this?</p></li>
  </ol>
  
  <p><strong>Reference</strong>:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/4555935/how-to-assign-null-to-a-sqlparameter">Assign null to a SqlParameter</a></li>
  <li><a href="https://stackoverflow.com/questions/13306275/datatype-returned-varies-based-on-data-in-table">Datatype returned varies based on data in table</a></li>
  <li><a href="https://stackoverflow.com/questions/13265704/conversion-error-from-database-smallint-into-c-sharp-nullable-int?lq=1">Conversion error from database smallint into C# nullable int</a></li>
  <li><a href="https://stackoverflow.com/questions/4488727/what-is-the-point-of-dbnull/9632050#9632050">What is the point of DBNull?</a>  </li>
  </ol>
  
  <p>CODE</p>
  
  <pre><code>    public Collection&lt;Log&gt; GetLogs(LogSearch logSearch)
      {
          Collection&lt;Log&gt; logs = new Collection&lt;Log&gt;();
  
          using (SqlConnection connection = new SqlConnection(connectionString))
          {
              connection.Open();
  
              string commandText = @"SELECT  *
                  FROM Application_Ex E 
                  WHERE  (E.application_ex_id = @application_ex_id OR @application_ex_id IS NULL)";
  
              using (SqlCommand command = new SqlCommand(commandText, connection))
              {
                  command.CommandType = System.Data.CommandType.Text;
  
                  //Parameter value setting
                  //command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID);
                  if (logSearch.LogID != null)
                  {
                      command.Parameters.AddWithValue("@application_ex_id", logSearch.LogID);
                  }
                  else
                  {
                      command.Parameters.AddWithValue("@application_ex_id", DBNull.Value );
                  }
  
                  using (SqlDataReader reader = command.ExecuteReader())
                  {
                      if (reader.HasRows)
                      {
                          Collection&lt;Object&gt; entityList = new Collection&lt;Object&gt;();
                          entityList.Add(new Log());
  
                          ArrayList records = EntityDataMappingHelper.SelectRecords(entityList, reader);
  
                          for (int i = 0; i &lt; records.Count; i++)
                          {
                              Log log = new Log();
                              Dictionary&lt;string, object&gt; currentRecord = (Dictionary&lt;string, object&gt;)records[i];
                              EntityDataMappingHelper.FillEntityFromRecord(log, currentRecord);
                              logs.Add(log);
                          }
                      }
  
                      //reader.Close();
                  }
              }
          }
  
          return logs;
      }
  </code></pre>