title: C# SQL Aggregate ExecuteScalar Return Query
body: <p>An SQL aggregate function counting number of name enteries in DB.</p>
  
  <pre><code>string cnnStr = ConfigurationManager.ConnectionStrings["LGFConnectionString"].ConnectionString;
  string mySQL = "SELECT COUNT(*) FROM  " + which.table + " WHERE " + which.column + " = ?pram;";
  string value = null;
  
  using (MySqlConnection cnn = new MySqlConnection(cnnStr))
  {
          using (MySqlCommand cmd = new MySqlCommand(mySQL, cnn))
          {
              MySqlParameter param = new MySqlParameter("?pram", MySqlDbType.VarChar, 128);
              param.Value = which.text;
              cmd.Parameters.Add(param);
  
              cnn.Open();
  
              value = cmd.ExecuteScalar() as string;
              value = cmd.ExecuteScalar().ToString();
  
              cnn.Close();
          }
  }
  </code></pre>
  
  <p>Notice that I have called <code>cmd.ExecuteScalar</code> twice. Interesting part is that the query returns different results.</p>
  
  <pre><code>value = cmd.ExecuteScalar() as string;
  </code></pre>
  
  <p>doesn't return the correct value. It returns null for both if name is present or missing in the name column.</p>
  
  <pre><code>value = cmd.ExecuteScalar().ToString();
  </code></pre>
  
  <p>returns correctly. This returns "1" if present and "0" if missing. </p>
  
  <p>In searching the web, I haven't found an understandable explanation.</p>
  
  <p>I have read that if name is missing from name column, then <code>cmd.ExecuteScalar</code> will return <code>null</code>.</p>
  
  <p>What is the difference between:</p>
  
  <pre><code>value = cmd.ExecuteScalar() as string;
  value = cmd.ExecuteScalar().ToString();
  </code></pre>
  
  <p>Thank you,
  deDogs</p>