I am trying to execute a sql query by passing a lists of keys as a parameter of keycodelist, but it doesn't work. I got KeyNotFoundException even though the value exists in database.
How should I fix this issue? Do I need to escape the parameter calling getKeyValue?
I called the function like this.
getKeyValue("'username', 'userid'")
private Function getKeyValue(ByVal keyList as String)
Dim output As Dictionary (Of String, String)
Dim systemkeysql As String = "select key_code, key_value from system_key_table where key_code in (@keycodelist) "
Try
Using connection As New SqlConnection(getConnectionString())
Using command As New SqlCommand(systemkeysql, connection)
With (command.Parameters)
.Add(New SqlParameter("@keycodelist", System.Data.SqlDbType.NVarChar)).Value = keylist
End With
connection.Open()
Using reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
output.Add(reader(0), reader(1))
End While
End Using
End Using
connection.Close()
End Using
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly)
End Try
Return Output
End Function