Reputation: 10913
I'm getting this error in vb.net, when trying to connect to mysql database:
An error occurred creating the form. See Exception.InnerException for details. The error is: The given key was not present in the dictionary.
I'm reading the database information (user, password, host, database) from a textfile.
Dim FILE_NAME As String = "D:\connection.txt"
Dim objReader As New StreamReader(FILE_NAME)
Public Sub textfileopener()
host = objReader.ReadLine
user = objReader.ReadLine
password = objReader.ReadLine
database = objReader.ReadLine
objReader.Close()
End Sub
Then assigning it to these variables:
Public host
Public database
Public user
Public password
Then finally I connect to the database:
Dim con As New MySqlConnection("Server=" + host + ";Database=" + database + ";Uid=" + user + ";Pwd=" + password + ";")
I think its not getting any values. Because if I assign values right ahead, I can connect without problems:
Public host = "localhost"
Public database = "db"
Public user = "root"
Public password = "pw
But if I do it this way then it will all be constant. And I cannot change anything at all. My problem is getting the correct values into the connection string. How do I do that?
Update
System.InvalidOperationException was unhandled
Message="An error occurred creating the form. See Exception.InnerException for details. The error is: The given key was not present in the dictionary."
Source="QueryGenerator"
StackTrace:
at QueryGenerator.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
at QueryGenerator.My.MyProject.MyForms.get_Form1()
at QueryGenerator.My.MyApplication.OnCreateMainForm() in C:\Users\Nrew\Documents\Visual Studio 2008\Projects\QueryGenerator\QueryGenerator\My Project\Application.Designer.vb:line 35
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at QueryGenerator.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Collections.Generic.KeyNotFoundException
Message="The given key was not present in the dictionary."
Source="mscorlib"
StackTrace:
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at MySql.Data.MySqlClient.MySqlConnectionStringBuilder.get_UserID()
at MySql.Data.MySqlClient.NativeDriver.Authenticate()
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at QueryGenerator.connection..ctor() in C:\Users\Nrew\Documents\Visual Studio 2008\Projects\QueryGenerator\QueryGenerator\connection.vb:line 22
at QueryGenerator.Form1..ctor() in C:\Users\Nrew\Documents\Visual Studio 2008\Projects\QueryGenerator\QueryGenerator\Form1.vb:line 5
InnerException:
Upvotes: 0
Views: 5126
Reputation: 185643
The error is coming from the MySQL client. It appears that it's expecting you to provide a particular parameter in the connection string that you are not. While I'm not familiar enough with the managed MySQL driver to tell you what's missing, the fact that something is missing is pretty clear.
You may want to try adding a space between the semicolon that signals the end of one parameter and the name of the next parameter. Like this:
Dim con As New MySqlConnection("Server=" + host + "; Database=" + database + "; Uid=" + user + "; Pwd=" + password + ";")
Upvotes: 1