'How do I fix the "The given key was not present in the dictionary"?

I am running a MySql 8 DB and using Visual Studio 2017. Here is my VB code.

Try
    With prm
        .Direction = ParameterDirection.Input
        .MySqlDbType = MySqlDbType.VarChar
        .Size = 20
        .ParameterName = "@Usr"
        .Value = strUser
    End With
    With prm2
        .Direction = ParameterDirection.Input
        .MySqlDbType = MySqlDbType.VarChar
        .Size = 20
        .ParameterName = "@Pwd"
        .Value = strPwd
    End With
    'With prm3
    '    .Direction = ParameterDirection.Output
    '    .MySqlDbType = MySqlDbType.Int16
    '    .ParameterName = "@UserID"
    'End With
    'With prm4
    '    .Direction = ParameterDirection.Output
    '    .MySqlDbType = MySqlDbType.VarChar
    '    .Size = 20
    '    .ParameterName = "@UserLogon"
    'End With
    With cmd
        .Parameters.Add(prm)
        .Parameters.Add(prm2)
        '.Parameters.Add(prm3)
        '.Parameters.Add(prm4)
        .CommandText = "GetUserLogon"
        .CommandType = CommandType.StoredProcedure
        .Connection = cnn
    End With

    'UserID,UserLogon
    'cmd.Parameters.Add("@UserID", SqlDbType.Int)
    'cmd.Parameters("@amt").Direction = ParameterDirection.Output
Catch ex As Exception
    Throw ex
End Try

dr = cmd.ExecuteReader

The code errors out on the above line with:

The given key was not present in the dictionary

This code has worked for years. Note that I tried adding output parameters, which are commented out above.

Here is the stored procedure

PROCEDURE `GetUserLogon`(Usr VARCHAR(20),Pwd VARCHAR(20))
BEGIN
    SELECT UserID,UserLogon FROM tblUsers
    WHERE UserLogon=Usr AND UserPwd=Pwd;
    -- UserLogon
END$$


Solution 1:[1]

Seems overly wordy code, consider:

Dim cs = " your connection string "

Using cn As New MySqlConnection(cs)
  Using cmd As New MySqlCommand("GetUserLogon",cn) With {.CommandType=CommandType.StoredProcedure}
    cmd.Parameters.AddWithValue("@Usr", strUser)
    cmd.Parameters.AddWithValue("@Pwd", strPwd)
    dr = cmd.ExecuteReader
  End Using
End Using

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fnostro