I want to count the number of rows in a MySql database and return the result in a label.
This is my code:
string connection = "SERVER= ###; PORT=###; DATABASE=###; UID=###; PASSWORD=###; CharSet=utf8;";
MySqlConnection mySqlCon = new MySqlConnection(connection);
MySqlCommand mySqlCmd = mySqlCon.CreateCommand();
mySqlCmd.CommandText = "Select count(*) as myCount from ###.###";
mySqlCon.Open();
int returnValue = (int) mySqlCmd.ExecuteScalar();
lblCLientNumber.Text = returnValue.ToString();
The error I get is:
Specified cast is not valid.
But when I run the SQL command in phpMyAdmin the result is accurate.
I'm not sure where am I making a mistake.
Console.WriteLine(typeof(mySqlCmd.ExecuteScalar()))?