0

So I am working on my very first database and using vb.net and mysql. I have followed some guides on how to connect the two (http://www.dreamincode.net/forums/topic/115753-use-vbnet-to-connect-to-mysql/ for example). However, when I run the code, I get my error message of "Cannot connect to the database". Here is the code that I am having trouble with. Note that I have followed the guide to the letter (other than the connection string). I believe my issue is with that but I am not positive. Also my database is pretty basic at this point and is simply named "Database".

Imports MySql.Data.MySqlClient

Public Class MainMenu
    Private Sub btnMultiple_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiple.Click
        ProFab.Show()
    End Sub

    Private Sub btnSingle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSingle.Click
        Dim MySqlConn As MySqlConnection
        MySqlConn = New MySqlConnection()
        MySqlConn.ConnectionString() = "database=Database"
        Try
            MySqlConn.Open()
            MessageBox.Show("Connection to Database has been opened.")
            MySqlConn.Close()
        Catch ex As Exception
            MessageBox.Show("Cannot connect.")
        Finally
            MySqlConn.Dispose()
        End Try
        SingleSearch.Show()
    End Sub

End Class
2
  • Never output a fixed/useless string as your error message, when you could have the DB TELL you what the problem is: catch ex ... messagebox.show(ex.ToString()) Commented Jul 28, 2014 at 14:14
  • 1
    I think you need to specify more details in your connectionstring. Take a look this site for better examples: connectionstrings.com/mysql Commented Jul 28, 2014 at 14:14

1 Answer 1

1

Try to display the Exception message instead.

Try
    cnx.Open()
Catch ex as Exception
    If ex.InnerException IsNot Nothing Then
        MessageBox.Show(ex.InnerException.Message)
    Else
        MessageBox.Show(ex.Message)
    End Try
Finally
    If cnx.State = ConnectionState.Open Then cnx.Close()
End Try

Or else, you may also use breakpoints and debug your code to have the proper Stack Trace which will provide you with as even more detailed information.

Also, you might want to check your connection string and make sure it's correct.

MySQL connection strings

Sign up to request clarification or add additional context in comments.

2 Comments

I ran your code and got the following message: "Access denied for user '@localhost' (using password: (NO)"
If your database is password protected, you need to provide it along with your user ID within your connection string. This either mean that your user ID and password are incorrect or not provided at all. This means that your connection string is incorrect. Please have a look at the link provided 'MySQL connection strings' and pick the one you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.