title: vb.net Running MySQL queries fast
body: <p>I have two listboxes in my windows form where the first one contains a bunch of IDs and when an ID is selected, all its properties from the MySQL database are displayed using the second listbox.</p>
  
  <p>My code:</p>
  
  <pre><code>Private Sub lstUsersKey_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstUsersKey.SelectedIndexChanged
      If (lstUsersKey.SelectedIndex.ToString IsNot Nothing) Then
          Dim UserID As Integer = CInt(lstUsersKey.SelectedItem.ToString)
  
          lstUsersValue.Items.Clear()
  
          lstUsersValue.Items.Add("User ID: " &amp; UserID)
          lstUsersValue.Items.Add("Forename: " &amp; frmLogin.User.GetForename)
          lstUsersValue.Items.Add("Surname: " &amp; frmLogin.User.GetSurname)
          lstUsersValue.Items.Add("Admin: " &amp; frmLogin.User.IsAdmin)
          lstUsersValue.Items.Add("Username: " &amp; frmLogin.User.GetUsername)
          lstUsersValue.Items.Add("Password: -") '&amp; frmLogin.User.GetPassword
          lstUsersValue.Items.Add("Phone Number: " &amp; frmLogin.User.GetPhoneNumber)
          lstUsersValue.Items.Add("Address: " &amp; frmLogin.User.GetAddress())
          lstUsersValue.Items.Add("Postcode: " &amp; frmLogin.User.GetPostcode())
          lstUsersValue.Items.Add("Email Address: " &amp; frmLogin.User.GetUserEmailAddress)
      End If
  End Sub
  </code></pre>
  
  <p>A function from my User class looks something like this but with different parameters:</p>
  
  <pre><code>Function GetPostcode()
      Return frmLogin.MySQL.SelectQuery("Postcode", "tbluser", "Username", "@username", Me.Username)
  End Function
  </code></pre>
  
  <p>My MySQL class contains this function:</p>
  
  <pre><code>Function SelectQuery(ByVal ColumnName As String, ByVal tblName As String, ByVal ConditionColumn As String, ByVal ConditionCheckOperator As String, ByVal ConditionCheckValue As String)
      Dim Query As String = "SELECT " &amp; ColumnName &amp; " " &amp; "FROM " &amp; tblName &amp; " " &amp; "WHERE " &amp; ConditionColumn &amp; "=" &amp; ConditionCheckOperator
      Dim Connection As New MySqlConnection(frmLogin.MySQL.ConnectionDetails)
      Dim Command = New MySqlCommand(Query, Connection)
      Connection.Open()
      Dim Result As Object
      Command.Parameters.AddWithValue(ConditionCheckOperator, ConditionCheckValue)
      Result = Command.ExecuteScalar
      Command.Dispose()
      Connection.Close()
      If Result IsNot Nothing Then
          Return Result.ToString
      Else
          Return ""
      End If
  End Function
  </code></pre>
  
  <p>The problem is no data is being loaded from my database and I'm not sure how to fix it and whether there is an easier way to run a lot of queries without having to make a new connection every time I want to run a query.</p>