Nayfun
Nayfun

Reputation: 31

Cannot connect, vb.net & mysql

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

Upvotes: 0

Views: 1446

Answers (1)

Will Marcouiller
Will Marcouiller

Reputation: 24132

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

Upvotes: 1

Related Questions