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

Edited 6 Years Ago by __avd: Added [code] tags.

This article has been dead for over six months. Start a new discussion instead.