0

Exception says Host 'XX' is not allowed to connect to this MySQL Server. I am using VB.Net, I have a Connect.vb class that connects to MySQL and it has a create database if not exists query.

MySqlConnection conn = new MySqlConnection("server=localhost;uid=root;pwd=root;");
        MySqlCommand pst = new MySqlCommand();
        try
        {
            conn.Open();
            pst.Connection = conn;
            pst.CommandText = "CREATE DATABASE IF NOT EXISTS StocksManagerDB";
            pst.ExecuteNonQuery();
            conn.Close();
            conn = new MySqlConnection("server=localhost;database=StocksManagerDB;uid=root;pwd=root;");
            conn.Open();
            conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Failure to connect to database.\nApplication will now exit.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            conn.Close();
            Application.Exit();
        }

The create database query executes properly. but when I log in at the login form it gives me that exception. I already tried this but it still gives me same exception. This is the code for the Login. in the load event i put this

conn = Connect.ConnectDB()

and this is the code when fo the button_click event in login

conn.Open();
            pst.CommandText = "SELECT * FROM tblLogin WHERE `Username` ='" + txtUsername.Text + "' AND `Password` ='" + txtPassword.Text + "'";
            rs = pst.ExecuteReader();
            if (rs.Read() == true)
            {
                Login_Success();
            }
            else
            {
                MessageBox.Show("Login Failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.Clear();
                txtPassword.Clear();
                txtUsername.Focus();
            }
            conn.Close();
Ajax
  • 15
  • 1
  • 5
  • please do not post your code as an image. Use Strg+K and copy your Code to your question – Rod Kimble Sep 14 '17 at 05:18
  • @RodKimble I have editted the post. Please have another look. Thank You. – Ajax Sep 14 '17 at 05:29
  • Can you post the code that accesses the database from the login form, as that is where your error is? It cannot be the above I think (else why are you connecting as root)? – Jonathan Willcock Sep 14 '17 at 05:56
  • Seems that your problem exists on something else you're not provided yet. Edit your question to include login form code which database access required to fetch/insert DB contents. – Tetsuya Yamamoto Sep 14 '17 at 09:12

1 Answers1

1

Open mysql workbench. Goto users and privileges. Replace the host for the user to '%'. You are using the root account create a user instead. Click add account at the bottom. Fill in the required information. Then click the tab schema privileges. Click add entry and add whatever this user is allowed to do and access. Use the user/password for your app.

You might want to limit the host anyway you should read a bit about mysql security. Spend the hour it requires.

Also you do not want to build your query by putting your parameters in the query. Read about sql injection attack.