0

I am trying to make a simple registration & activation system using my mysql server and vb.net and I am using the code below:

Imports MySql.Data.MySqlClient

Public Class ActivateMe
Dim MysqlConn As MySqlConnection
Dim myAdapter As New MySqlDataAdapter
Dim myData As MySqlDataReader

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MysqlConn = New MySqlConnection()
    Try
        MysqlConn.Open()
        Dim checkUsername As String = "SELECT COUNT(*) FROM users WHERE verif=@p1 and username=@p2"
        Dim insertData As String = "INSERT INTO users(hasVerif) VALUES(@p3)"

        Using MysqlConn = New MySqlConnection(mysqlconntxt4reg)
            Using myCommand = New MySqlCommand(checkUsername, MysqlConn)
                MysqlConn.Open()
                myCommand.Parameters.AddWithValue("@p1", TextBox1.Text)
                myCommand.Parameters.AddWithValue("@p2", currentRegUser)
                Dim result = myCommand.ExecuteScalar()
                If result IsNot Nothing AndAlso Convert.ToInt32(result) > 0 Then
                    Using myCommand2 = New MySqlCommand(insertData, MysqlConn)
                        myCommand.Parameters.AddWithValue("@p3", 1)
                        myCommand2.ExecuteNonQuery()
                        MsgBox("Successfully Activated! You May Now Login!", MsgBoxStyle.Information, "Success")
                        Me.Close()
                    End Using
                Else
                    MsgBox("Invalid Activation Code", MsgBoxStyle.Critical, "Error")
                End If
            End Using
        End Using
        MysqlConn.Close()
    Catch myerror As MySqlException
        MessageBox.Show("Cannot connect to database: " & vbNewLine & vbNewLine & myerror.Message)
    Finally
        MysqlConn.Dispose()
    End Try

End Sub
End Class

The program sends the activation code to the email successfully and uploads the activation code to the database for checking but when I am on the activation form and enter the code, it says:

Host (my pc name) is not allowed to connect to this mysql server

In the different forms, the server accepts the connection and allows access to the database so I don't see why it would be different here... Please send me help! Thanks rodit

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Obviously the error doesn't have the ide colours like it does above... –  Nov 16 '13 at 22:23
  • In the other forms, do you have the same New MySqlConnection() statement? – Andrei B Nov 17 '13 at 12:25
  • Duplicate of [Host 'xxx.xx.xxx.xxx' is not allowed to connect to this MySQL server](http://stackoverflow.com/questions/1559955/host-xxx-xx-xxx-xxx-is-not-allowed-to-connect-to-this-mysql-server). – CodeCaster Nov 17 '13 at 12:39
  • CodeCaster, one question has to do with MySQL permissions and mysql client program, while the other with VB.NET and bug in code? Where's the duplication? – Andrei B Nov 17 '13 at 21:03
  • @Andrei you're right, I only read the error. – CodeCaster Nov 18 '13 at 08:50

1 Answers1

0

I'm not very familiar with MySQL under .NET but I believe you do something a bit odd here. First you open a connection without a connection string (I assume that uses default parameters)

MysqlConn = New MySqlConnection()
Try
    MysqlConn.Open()

and then you open another connection using an explicit string

Using MysqlConn = New MySqlConnection(mysqlconntxt4reg)
    Using myCommand = New MySqlCommand(checkUsername, MysqlConn)
        MysqlConn.Open()

Notice it's the same MysqlConn variable (with different objects).

So the MySqlException you're catching could be coming from any one of them. You can use that exception to find out which of the open calls actually triggered the exception (I assume it's first one).

You should probably also cleanup code and keep only one open call.

Andrei

Andrei B
  • 2,740
  • 17
  • 12
  • Just curious, did the first open throw the exception? Also, please mark answer as accepted if it helped you. – Andrei B Nov 17 '13 at 21:04