Error in my test VB app for connecting to MySQL Database

Status
Not open for further replies.

nwinteractive

New Member
Messages
6
Reaction score
0
Points
0
Ok, I have made a test database and loaded it with some data. In my test app I am trying to connect to the database but it throws a MySqlException when it hits the line of code conn.Open(). Can someone look at my code and see where I went wrong. Thanks.

VB CODE
Code:
Imports MySql.Data.MySqlClient
Partial Class dbtest
    Inherits System.Web.UI.Page
  Protected Sub Sql1Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'Connect to database
        Dim conn As MySqlConnection
        Dim myData As MySqlDataReader
        Dim cmd As MySqlCommand
        Dim ConnectionString As String = "Server=mysql-chopin.x10hosting.com;Database=nwintera_nightwing;Uid=nwintera_test1234;Pwd=12345;"
        conn = New MySqlConnection(ConnectionString)
        'see if connection failed.
        Try
            conn.Open()
            ConnLabel.Text = "Connected to databse!"
            'sql query
            cmd = New MySqlCommand("SELECT key FROM test WHERE key = 0", conn)
            'start query
            myData = cmd.ExecuteReader()
            'see if key exits.
            If myData.HasRows = 0 Then
                MsgTextBox.Text = "Invalid Authentication Key!"
            End If

        Catch ex As MySqlException
            ConnLabel.Text = "Error connecting to database!"
        End Try
    End Sub
End Class
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The server name should be "localhost", not "mysql-chopin.x10hosting.com". Other than that, you'll need to find out and post the specific error message from the MySQLException.

For testing a simple app, placing the connection string in the code is fine, but for anything else you should use web.config.

The password in your code sample better not be the real password, both because it's too simple and you shouldn't post passwords.
 

nwinteractive

New Member
Messages
6
Reaction score
0
Points
0
Ok when I run it now with localhost I get a 500 error.
FYI: The user id and password posted are not the real ones.
Code:
[B][I]Keyword not supported. Parameter name: localhost;database[/I][/B]

 [B]Description: [/B]HTTP 500. Error processing request.
 [B]Stack Trace: [/B]
 System.ArgumentException: Keyword not supported.
Parameter name: localhost;database
  at MySql.Data.MySqlClient.MySqlConnectionStringBuilder.ValidateKeyword (System.String keyword) [0x00000] in <filename unknown>:0 
  at MySql.Data.MySqlClient.MySqlConnectionStringBuilder.set_Item (System.String keyword, System.Object value) [0x00000] in <filename unknown>:0 
  at System.Data.Common.DbConnectionStringBuilder.ParseConnectionStringNonOdbc (System.String connectionString) [0x00000] in <filename unknown>:0 
  at System.Data.Common.DbConnectionStringBuilder.ParseConnectionString (System.String connectionString) [0x00000] in <filename unknown>:0 
  at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString (System.String value) [0x00000] in <filename unknown>:0 
  at MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor (System.String connStr) [0x00000] in <filename unknown>:0 
  at MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString (System.String value) [0x00000] in <filename unknown>:0 
  at MySql.Data.MySqlClient.MySqlConnection..ctor (System.String connectionString) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) MySql.Data.MySqlClient.MySqlConnection:.ctor (string)
  at dbtest.Sql1Button_Click (System.Object sender, System.EventArgs e) [0x00000] in <filename unknown>:0 
  at System.Web.UI.WebControls.Button.OnClick (System.EventArgs e) [0x00000] in <filename unknown>:0 
  at System.Web.UI.WebControls.Button.RaisePostBackEvent (System.String eventArgument) [0x00000] in <filename unknown>:0 
  at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent (System.String eventArgument) [0x00000] in <filename unknown>:0 
  at System.Web.UI.Page.RaisePostBackEvent (IPostBackEventHandler sourceControl, System.String eventArgument) [0x00000] in <filename unknown>:0 
  at System.Web.UI.Page.RaisePostBackEvents () [0x00000] in <filename unknown>:0 
  at System.Web.UI.Page.ProcessRaiseEvents () [0x00000] in <filename unknown>:0 
  at System.Web.UI.Page.InternalProcessRequest () [0x00000] in <filename unknown>:0 
  at System.Web.UI.Page.ProcessRequest (System.Web.HttpContext context) [0x00000] in <filename unknown>:0
 

nwinteractive

New Member
Messages
6
Reaction score
0
Points
0
Ok. I fixed the error in my last post but now I get a new 500 Error.
This is the connection string I'm using:
Code:
Dim ConnectionString As String = "Server=localhost;Database=nwintera_nightwing;Userid=??????;Password=???????;"

Code:
[B]Server Error in '/' Application[/B]

[B][I]Connection refused[/I][/B]

 [B]Description: [/B]HTTP 500. Error processing request.
 [B]Stack Trace: [/B]
 System.Net.Sockets.SocketException: Connection refused
  at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0 
  at System.Net.Sockets.Socket+Worker.Connect () [0x00000] in <filename unknown>:0   [B]Version information: [/B] Mono Runtime Version: 2.6.1 (tarball Sat Mar 20 23:25:36 EDT 2010); ASP.NET Version: 2.0.50727.1433
 
Status
Not open for further replies.
Top