2

Not sure why I get an error every single time i try to connect. I tried so many things. When i type the wrong password while connecting manually it says access not granted or whatever so I know it connects but when it connects I get this weird error.

An unhandled exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll Additional information: The given key was not present in the dictionary.

For localhost it works when I have this connection string.

server=localhost; port=3306; USER ID=root; password=***; database=database;

but when I change the server user id and password it decides to not work.

The exception is thrown at cn.Open();

     private void button1_Click(object sender, EventArgs e)
    {
        MySqlConnection cn = new MySqlConnection();
        cn.ConnectionString = "server=db4free.net ;port=3306; 
                               user=goof; password=pwd; database=s;";
        cn.Open();
        MySqlCommand cmd = new MySqlCommand("select * from users where
                                             username = '" 
                                             + textBox1.Text + "',
                                             and password = '" 
                                             + textBox2.Text + "'", cn);
        MySqlDataReader dr;
        dr = cmd.ExecuteReader();
        int count = 0;

        while(dr.Read())
        {
            count += 1;
        }
        if(count == 1)
        {
            MessageBox.Show("success");
        }
        else
        {
            MessageBox.Show("error");
        }
    }
pijemcolu
  • 2,257
  • 22
  • 36
  • As long as I know, MySQL using "Uid and Pwd" for username and password, not "user and password" in connection strings. –  Jul 02 '16 at 09:39
  • ya i already tried that, so lost right now :| –  Jul 02 '16 at 09:48
  • Try this > http://stackoverflow.com/questions/2176329/error-in-mysql-connection-the-given-key-was-not-present-in-the-dictionary Wrong connection string "pooling=false" –  Jul 02 '16 at 09:55

2 Answers2

1

The problem is the user=goof field should be user id=goof.

Change your connection string into this:

cn.ConnectionString = "server=db4free.net ;port=3306; USER ID=goof; password=pwd; database=s;";
pijemcolu
  • 2,257
  • 22
  • 36
0

try using "user id" or "uid" instead of just "user" and let us know if its does the job!

Olfredos6
  • 808
  • 10
  • 19
  • none of the suggestions worked. im very confused why this wont work. –  Jul 02 '16 at 20:03