title: SQL Server "SELECT COUNT(field) FROM tblname" returns null
body: <p>This is pretty strange.  I have a very simple SQL query in my c# application, which counts records in a SQL Server table.  The code snippet is as follows:</p>
  
  <pre><code>    // Returns the number of ports defined for a given slot.
      int returnval = 0;
      connection.Open();
      SqlCommand command = new SqlCommand("SELECT COUNT(Slotted_Port_UID) FROM tblSlottedCardPorts WHERE Slot_ID = @SlotID ", connection);
      command.Parameters.AddWithValue("@SlotID", EquipmentSlotID);
      SqlDataReader reader = command.ExecuteReader();
      if (reader.HasRows)
      {
          if (!reader.IsDBNull(0))
          {
              returnval = reader.GetInt32(0);
          }
      }
      reader.Close();
      connection.Close();
      return returnval;
  </code></pre>
  
  <p>When I copy and paste the SQL statement directly in to SQL server mgmt studio, the query returns as expected, returning 0 or a positive integer, depending on the number of rows.  However, though the code passes the .HawRows condition, it fails on the reader.IsDBNull(0) condition.  The tblSlottedCardPorts exists, and the Slotted_Port_UID column is a unique integer.  Any suggestions on this?  I can't see why COUNT would ever return a Null value, since no joins or other factors are involved.</p>
  
  <p>Thanks for any advice with this!</p>
  
  <p>Mitchell</p>