VB.net 'System.Collections.Generic.KeyNotFoundException' exception

618 views Asked by At

Here is my code:

Imports System.IO
Imports MySql.Data.MySqlClient

Public Class F_login

    Dim strServer As String = "localhost"
    Dim strDB As String = "lab_utilization"
    Dim strUser As String = "root"
    Dim strPw As String = ""

    Dim cs As String = "Server=$($strServer);Port=3306;Database=$($strDB);Uid=$($strUser);Pwd=$($strPw);"
    Dim con As New MySqlConnection(cs)


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim id As Integer = uid.Text

        Dim pswd As New MySqlCommand("Select Password from authentication where id = " & id & "", con)
        pswd.ExecuteNonQuery()

        Dim mysqladapter As New MySqlDataAdapter(pswd)
        Dim dt As New DataTable
        mysqladapter.Fill(dt)
        dg.DataSource = dt
        Dim psd As String = dg.Rows(0).Cells(0).Value

        If pswd1.Text = psd Then
            Me.Visible = False
            F_Main_screen.Show()
        Else
            MessageBox.Show("Invalid ID or Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Sub F_login_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        con.ConnectionString = cs
        con.Open()
    End Sub
End Class

The following exception occurs on con.Open():

An unhandled exception of type System.Collections.Generic.KeyNotFoundException' occurred in MySql.Data.dll Additional information: Keyword 'address' not found.

Any assistance will be appreciated, thank you.

2

There are 2 answers

0
WSC On

I don't think your syntax for string interpolation is correct.

Try changing your connection string to:

`Dim cs As String = "Server=${strServer};Port=3306;Database=${strDB};Uid=${strUser};Pwd=${strPw};"

0
JRichardsz On

This is when you are passing an empty host instead the localhost or ip of your mysql

In my case, the host var was empty

$"Server={host};Port={port};Database={databaseName};
Uid={user};Pwd={password};Pooling={poolEnabled};
Min Pool Size={minPoolSize};Max Pool Size={maxPoolSize};
Connection Timeout={poolConnTimeout};
Connection Lifetime={poolConnLifetime}";

I used \n in the connection string to improve reading