[SOLVED] .Net MySql error "The given key was not present in the dictionary"

Issue

Trying to get simple count from table results in exception bellow.
Tried different select statemens which also makes exception: “SELECT * FROM goods“, but “SELECT col1, col2 FROM goods” – works without exception. What am I doing wrong? From workbench these selects works.

The given key was not present in the dictionary.
System.Collections.Generic.KeyNotFoundException: The given key was not
present in the dictionary. at
System.Collections.Generic.Dictionary`2.get_Item(TKey key) at
MySql.Data.MySqlClient.MySqlField.SetFieldEncoding() at
MySql.Data.MySqlClient.NativeDriver.GetColumnData(MySqlField field)
at MySql.Data.MySqlClient.NativeDriver.GetColumnsData(MySqlField[]
columns) at MySql.Data.MySqlClient.Driver.GetColumns(Int32 count)
at MySql.Data.MySqlClient.ResultSet.LoadColumns(Int32 numCols) at
MySql.Data.MySqlClient.ResultSet..ctor(Driver d, Int32 statementId,
Int32 numCols) at MySql.Data.MySqlClient.Driver.NextResult(Int32
statementId, Boolean force) at
MySql.Data.MySqlClient.MySqlDataReader.NextResult() at
MySql.Data.MySqlClient.MySqlDataReader.Close() at
MySql.Data.MySqlClient.MySqlCommand.ResetReader() at
MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior
behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at MySqlSybaseComparer.DbTester.Test(String& error) in
c:\MySqlSybaseComparer\DbTester.cs:line 68

code snippet:

using (MySqlConnection conn = new MySqlConnection(ConStrMySql))
{
    try
    {
        conn.Open();
        using (MySqlCommand cmd = new MySqlCommand("SELECT count(*) FROM goods", conn))
        {
            using (MySqlDataReader reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                    MessageBox.Show(reader[0].ToString());
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + Environment.NewLine + ex.ToString(););
    }
    conn.Close();
}

Connection string to DB: Server=localhost; Database=art; Uid=ramunas; Pwd=xxxx; AllowUserVariables=True;

Solution

Code is correct and all suggestion also should work. Just removed “collation-server” settings and restarted server and everything works as expected.

enter image description here

Answered By – Ramunas

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.