Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

mysql

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;

like image 585
Ramunas Avatar asked Jan 30 '17 06:01

Ramunas


People also ask

Why was the given key was not present in the Dictionary?

An System.Collections.Generic.KeyNotFoundException “ The given key was not present in the dictionary ” can be the result of using a too old MySQL Connector/NET version in your ASP.NET web application.

How to solve the '0 was not found in Dictionary' error?

And if you are working with ADO.NET model be careful the versions of component that you are installing and referencing. For anyone with the error "the given key '0' was not found in the dictionary", it seems it's looking for the MySQL DLL. Adding MySql.Data through your package manager or adding the DLL manually from the connector should solve it.

Is ISNULL() leagal in MySQL?

isnull (count (*),0) - is not leagal in mysql, anyway count () should never return null. I hardcoded my connection string into code, and since i can get readout from database i am sure connection string is correct. I have solved your same error simply adding the charset to the connection string:


2 Answers

I have solved your same error simply adding the charset to the connection string:

Server=myServer;Port=3306;Database=myDB15;User ID=usr33;Password=usr33P;CharSet=utf8;

In my case I'm using MySql Connector for .Net version 6.9.3. to connect to 30 equal databases with the same structure, same collation (utf8_unicode_ci) and different table contents.

When I ran MySqlCommand.ExecuteReader() method to select content from user table, in some databases (4 of 30) a got the same error The given key was not present in the dictionary.

like image 142
Rafael Neto Avatar answered Oct 26 '22 22:10

Rafael Neto


After spending 5 hours researching how to fix it!!!.. i finally figured it out... all you need to do is to make sure that your 'MySql.Data.dll' is up to date! you can download it somewhere. or you can find it here C:\Program Files (x86)\MySQL\MySQL Installer for Windows\MySql.Data.dll.. :D

like image 44
Marvin Nario Machitar Avatar answered Oct 27 '22 00:10

Marvin Nario Machitar