title: Specified cast is not valid error in C#
body: <p>How do I resolve this?</p>
  
  <pre><code>MySqlConnection con = new MySqlConnection(myconnectionstring);
  string validateinventory = "SELECT COUNT(*) FROM inventory_register WHERE barcode = @barcode";
  
  MySqlCommand cmd = new MySqlCommand(validateinventory,con);
  
  con.Open();
  cmd.Parameters.AddWithValue("@barcode", textBox3.Text);
  int Result = (int)cmd.ExecuteScalar();
  con.Close();
  
  if (Result &gt; 0)
  {
      textBox3.Text = "";
      MessageBox.Show("Invalid Barcode! Please Register Inventory or Enter Valid Barcode");
  }
  </code></pre>
  
  <p>An InvalidCastException is thrown in the following line:</p>
  
  <pre><code>int Result = (int)cmd.ExecuteScalar();
  </code></pre>