title: Fix the VB.NET error "Connection must be valid and open"
body: <p>I am getting an error in this login form which I have created which is "Connection must be valid and open, can someone help me, here is the code. I am unable to understand what this error is and I need your help.</p>
  
  <pre><code>Imports MySql.Data.MySqlClient
  Public Class LoginForm1
  
      Dim Username As String
      Dim Password As String
      Public StudentName As String
      'MySql
      Dim connection As New MySqlConnection("Server=server;User ID=id;Password=pass;Database=data;SSLMode=None")
  
      Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
  
          Username = UsernameTextBox.Text
          Password = PasswordTextBox.Text
          StudentName = NameTextBox.Text
  
          Dim command As New MySqlCommand("SELECT `Username`, `Password`, `FirstName` FROM `people` WHERE `Username` = @username AND `Password` = @password And `FirstName` = @name", connection)
          command.Parameters.Add("@username", MySqlDbType.VarChar).Value = Username
          command.Parameters.Add("@password", MySqlDbType.VarChar).Value = Password
          command.Parameters.Add("@name", MySqlDbType.VarChar).Value = StudentName
  
  
          Dim adapter As New MySqlDataAdapter(command)
          Dim table As New DataTable()
          Dim reader As MySqlDataReader = command.ExecuteReader
  
          adapter.Fill(table)
  
          If table.Rows.Count = 0 Then
              MessageBox.Show("Invalid Username Or Password", "Invalid Login", MessageBoxButtons.OK, MessageBoxIcon.Information)
          Else
  
              MessageBox.Show("Logged In", "Logged in Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information)
              MainMenuForm.Show()
              Me.Close()
          End If
      End Sub
  
      Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
          Me.Close()
      End Sub
  
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
          Dim webAddress As String = "http://windowsadvancedstudy.com"
          Process.Start(webAddress)
      End Sub
  End Class
  </code></pre>