0

I am connecting to MySQL database in C#. I am using 100 percent correct user name and password but I get invalid username/password or error no 1045. I have tried deleting the database and users and recreated all these and assigned all permissions in cpanel but failed. It works on my computer localhost(virtual server). Tried so many times. please help. My code is

        string myConnectionString = @"server=mydomain.co.uk;userid=MyUserID;
        password=PasswordOfUser;database=databasename";
        MySqlConnection conn = null; 
        try
        {
            //MySqlConnection 
            conn = new MySqlConnection(myConnectionString);
            conn.Open();
            MessageBox.Show("OK");
            conn.Close();
        }
        catch (MySqlException ex)
        {
            switch (ex.Number)
            {
                case 0:
                    MessageBox.Show("Cannot connect to server.  Contact administrator");
                    break;
                case 1042:
                    MessageBox.Show("Can't get hostname address. Check your internet connection. If does not solve, contact Administrator");
                    break;
                case 1045:
                    MessageBox.Show("Invalid username/password");
                    break;
            }
        }
Imran Kanjoo
  • 57
  • 2
  • 13

2 Answers2

0

Error 1045 isn't necessarily just for invalid user/passwd combos. It can indicate that the username you are using to log in does not have the required permissions to access this database.

Try (as root):

GRANT ALL ON database.* TO username@your-ip IDENTIFIED by 'password';

or

GRANT ALL ON database.* TO username@localhost IDENTIFIED by 'password';

If you'd like to restrict this user's actions, modify ALL to the specific actions the user is allowed to perform, e.g. SELECT, INSERT, DELETE, etc. (use a comma-separated list for multiple actions).

Kyle G.
  • 870
  • 2
  • 10
  • 22
  • thanks @Kyle whenever I create the database and user I give all permissions in cpanel. I did this in php some time ago and it worked at some other domain. can you please help me out to by writing a complete code for granting permission in csharp. – Imran Kanjoo Aug 27 '13 at 12:00
  • Granting permissions is done just like any other database query, which means that you first have to be able to connect. Since connecting seems to be your problem, I can't think of any other C# code that would help. Can you post the permissions for the userID you are using in this situation? – Kyle G. Aug 27 '13 at 14:18
0

Enable remote access in cpanel by adding host(IP OR DOMIAN etc)

Imran Kanjoo
  • 57
  • 2
  • 13