Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        exitwithout()
        Me.Close()
    End Sub

Private Sub exitwithout()
        myCommand.Connection = myConnection
        myConnection.Open()
        myCommand.CommandText = "Select * from orders where o_id= " & txtb1.Text & ""
        dr = myCommand.ExecuteReader()
        Try
            If dr.HasRows Then
                deletewithout()
            Else
                Exit Sub
            End If
        Catch ex As Exception
            MessageBox.Show("Error while Deleting Record..." & ex.Message, "Delete Records")
        Finally
            myConnection.Close()
        End Try
    End Sub

Private Sub deletewithout()
        Try
            Dim intTemp As Integer
            intTemp = MsgBox("Are You Sure, You want to Exit Without Purchase ?", MsgBoxStyle.YesNo)
            If intTemp = 6 Then
                myConnection.Open()
                myCommand.Connection = myConnection
                myCommand.CommandText = "delete from orders where o_id = " & txtb1.Text & ""
                i = myCommand.ExecuteNonQuery()
                If i > 0 Then
                    myConnection.Close()
                    MessageBox.Show("Order Cancelled Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            Else
                Exit Sub
            End If
        Catch ex As Exception
            MessageBox.Show("Error while Deleting Record..." & ex.Message, "Delete Records")
        Finally
            myConnection.Close()
        End Try
    End Sub

Connection will be busy till dataReader is not closed.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        exitwithout()
        Me.Close()
    End Sub

    Private Sub exitwithout()
        myCommand.Connection = myConnection
        myConnection.Open()
        myCommand.CommandText = "Select * from orders where o_id= " & val(txtb1.Text)
        dr = myCommand.ExecuteReader()

        Dim isfound as boolean = false
  
        If dr.Read() Then
             isfound=true
        end if
        dr.close()
        myConnection.Open()
         
        If isFound Then
                deletewithout()
        End If
    End Sub

    Private Sub deletewithout()
            Dim intTemp As Integer
            intTemp = MsgBox("Are You Sure, You want to Exit Without Purchase ?", MsgBoxStyle.YesNo)
            If intTemp = 6 Then
                myCommand.Connection = myConnection
                myCommand.CommandText = "delete from orders where o_id = " & val(txtb1.Text)
                myConnection.Open()
                i = myCommand.ExecuteNonQuery()
                myConnection.Close()
                If i > 0 Then
                    MessageBox.Show("Order Cancelled Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            End If
    End Sub

pls my code gives me this error "vb.net: the connection was not closed. the connections current state is open"............. this is the code below

Private Sub Display()

    Try

        SqlCon.Open()

        Dim da As New SqlDataAdapter(("select GradeID, DiffAmount from pr_Sal_Struct_Hdr where GradeID ='" & Trim(DataGridView1.CurrentRow.Cells(0).Value) & "'"), SqlCon)
        Dim i As Integer
        i = DataGridView1.CurrentRow.Index
        Dim dt As New DataTable
        da.Fill(dt)
        If dt.Rows.Count > 0 Then
            frmSalaryStructure.TextBox1.Text = DataGridView1.Item(0, i).Value.ToString
            frmSalaryStructure.TextBox2.Text = DataGridView1.Item(1, i).Value.ToString
            frmSalaryStructure.Load_Gridview()

        End If

    Catch ex As Exception
        MsgBox(ex.Message)
    Finally

        If SqlCon.State = ConnectionState.Open Then SqlCon.Close()

    End Try
End Sub

and this one too

Public Sub Load_Gridview()
    Dim SqlOrder As New SqlCommand()
    Dim da As New SqlDataAdapter
    Dim dt As New DataTable
    Dim strSearch As String
    strSearch = TextBox1.Text
    Try
        SqlCon.Open()

        SqlOrder.Connection = SqlCon
        SqlOrder.CommandType = CommandType.Text
        SqlOrder.CommandText = "SELECT * FROM pr_Sal_Struct_dtl where GradeID='" & strSearch & "'"
        da.SelectCommand = SqlOrder
        da.Fill(dt)
        DataGridView1.Refresh()

        Me.DataGridView1.DataSource = dt

    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally

        If SqlCon.State = ConnectionState.Open Then sqlcon.Close()

    End Try
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.