I've got this code:
private int GetCombinedRankRank(int combinedRankValue)
{
    try
    {
        conn = new MySqlConnection(connstr);
        conn.Open();
        try
        {
            DataTable dtStates = new DataTable();
            MySqlCommand comm = conn.CreateCommand();
            comm.CommandText = "SELECT COUNT(states_id) "+
                               "FROM states "+
                               "WHERE combinedRank < @combinedRankValue";
            comm.Parameters.AddWithValue("@combinedRankValue", combinedRankValue);
            return (int)comm.ExecuteScalar();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            throw;
        }
    }
    finally
    {
        conn.Close();
    }
}
...which fails on the line:
return (int)comm.ExecuteScalar();
...with "Specified cast is not valid" -- Yet the same SQL works in Database Workbench:
The SQL returns an int. The data type of the combinedRank column in the Table is Integer. I am casting that value to an int. How can that be an invalid cast?

 
     
     
     
    