VB.NET: Too many connections
<p>While testing my program, an error popped up saying, "Too many connections" when I tried to log in an another user. </p>

<p>I need to fix it so that the connections aren't left open to time out on their own, so that it runs perfectly. </p>

<pre><code> Private Sub addstub()
    Using connection As New MySqlConnection(connectionstring)
        SQL = "SELECT count(*) from remaining_ham where Stub=@stub and Emp_No LIKE '%" &amp; txtid.Text &amp; "%'"
        Using Command As New MySqlCommand(SQL, connection)
            Command.Parameters.AddWithValue("@stub", txtclaim.Text)
            Command.Parameters.AddWithValue("@claim", "CLAIMED")
            Command.CommandText = SQL
            connection.Open()
            Dim count As Integer = Command.ExecuteScalar

            If count = 1 Then
                MsgBox("PROCESSING")
                Using connection2 As New MySqlConnection(connectionstring)
                    SQL = "Select count(*) from  remaining_ham where status='CLAIMED' and Stub='" &amp; txtclaim.Text &amp; "' and Emp_No LIKE'%" &amp; txtid.Text &amp; "%' "
                    Using command2 As New MySqlCommand(SQL, connection)
                        connection2.Open()
                        Dim i1 As Integer = command2.ExecuteScalar()
                        If i1 = 1 Then
                            MsgBox("ALREADY CLAIMED")
                        Else
                            Using connection3 As New MySqlConnection(connectionstring)
                                SQL = "Select Stub,Total,Brickham,Jamon,Fiesta,status from remaining_ham where Stub='" &amp; txtclaim.Text &amp; "'  and Emp_No LIKE'%" &amp; txtid.Text &amp; "%' "
                                Using myAdapter As New MySqlDataAdapter(SQL, connection)
                                    Dim table = New DataSet
                                    myAdapter.Fill(table)

                                    txtbrick.Text = table.Tables(0).Rows(0)("Brickham").ToString
                                    txtjamon.Text = table.Tables(0).Rows(0)("Jamon").ToString
                                    txtfiesta.Text = table.Tables(0).Rows(0)("Fiesta").ToString
                                    txttotal.Text = table.Tables(0).Rows(0)("Total").ToString

                                    If MsgBox("ARE YOU SURE?" + Environment.NewLine + "Stub No: " + txtid.Text + Environment.NewLine + "Brickham: " + txtbrick.Text + Environment.NewLine + "Jamon De Bola: " + txtjamon.Text + Environment.NewLine + "Fiesta Ham: " + txtfiesta.Text, MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                                        'MsgBox("TAMA")
                                        jam = CDbl(txtjamon.Text)
                                        rmham = CDbl(txtremjamon.Text)
                                        txtremjamon.Text = (rmham - jam).ToString
                                        Dim dbjam = Format(CDbl(txtremjamon.Text), "#,###")
                                        If dbjam = "" Then
                                            dbjam = 0
                                        End If

                                        brk = CDbl(txtbrick.Text)
                                        rembrk = CDbl(txtrembrick.Text)
                                        txtrembrick.Text = (rembrk - brk).ToString
                                        Dim dbbrick = Format(CDbl(txtrembrick.Text), "#,###")
                                        If dbbrick = "" Then
                                            dbbrick = 0
                                        End If

                                        fiesta = CDbl(txtfiesta.Text)
                                        rmfiesta = CDbl(txtremfiesta.Text)
                                        txtremfiesta.Text = (rmfiesta - fiesta).ToString
                                        Dim dbfiesta = Format(CDbl(txtremfiesta.Text), "#,###")
                                        If dbfiesta = "" Then
                                            dbfiesta = 0
                                        End If
                                        total = CDbl(txttotal.Text)
                                        rmtotal = CDbl(txtremtotal.Text)
                                        txtremtotal.Text = (rmtotal - total).ToString
                                        Dim dbtotal = Format(CDbl(txtremtotal.Text), "#,###")
                                        If dbtotal = "" Then
                                            dbtotal = 0
                                        End If

                                        Using connection4 As New MySqlConnection(connectionstring)
                                            SQL = "UPDATE order_ham SET rem_brick='" &amp; dbbrick &amp; "', rem_jam='" &amp; dbjam &amp; "', rem_fiesta='" &amp; dbfiesta &amp; "', rem_total='" &amp; dbtotal &amp; "' where Emp_No=" &amp; txtid.Text &amp; "  "
                                            Using command3 As New MySqlCommand(SQL, connection4)
                                                connection4.Open()
                                                Dim i As Integer = command3.ExecuteNonQuery
                                                If i = 0 Then
                                                    MsgBox("WRONG")
                                                    Exit Sub
                                                Else
                                                    ' MsgBox("RIGHT")
                                                    Using connection5 As New MySqlConnection(connectionstring)
                                                        Dim date1 As Date = Date.Today
                                                        SQL = "UPDATE remaining_ham SET status='CLAIMED',ddate='" &amp; DateTime.Now &amp; "' where Stub='" + txtclaim.Text + "' and Emp_No LIKE '%" + txtid.Text + "%'  "
                                                        Using command4 As New MySqlCommand(SQL, connection5)
                                                            connection5.Open()
                                                            Dim a As Integer = command4.ExecuteNonQuery
                                                            connection5.Close()
                                                            If a = 0 Then
                                                                MsgBox("not claim: ERROR ")
                                                                Exit Sub
                                                            Else
                                                                MsgBox("SUCCESS")
                                                                Using da As New MySqlDataAdapter(SQL, connection5)
                                                                    Dim dt As New DataTable
                                                                    da.Fill(dt)
                                                                    MetroGrid1.DataSource = Nothing
                                                                    MetroGrid1.Rows.Clear()
                                                                    MetroGrid3.DataSource = Nothing
                                                                    MetroGrid3.Rows.Clear()
                                                                    loadRemainingHam()
                                                                    remainingorder()
                                                                    txtclaim.Focus()
                                                                End Using


                                                            End If


                                                        End Using
                                                    End Using

                                                End If
                                            End Using
                                        End Using




                                    Else
                                        MsgBox("CANCELLED")
                                    End If

                                End Using
                            End Using
                        End If

                    End Using
                End Using
            Else
                MsgBox("ERROR")
            End If

        End Using
    End Using
End Sub
</code></pre>

<p>Thank you for those who will answer. </p>