I would like to DELETE more than one record from DB , using Asp.net vb & MS Access

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim cell As TableCell = e.Row.Cells(4)
            Dim cell2 As TableCell = e.Row.Cells(6)
            Dim mytxtboxdepart As TextBox = e.Row.FindControl("txtBoxDeparture")
            Dim mytxtboxstays As TextBox = e.Row.FindControl("txtBoxStays")
            Dim mytxtboxcriteria As TextBox = e.Row.FindControl("txtBoxCriteria")

            Dim provider As String
            Dim connString As String
            Dim myconnection As OleDbConnection = New OleDbConnection
            provider = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("~/App_Data/myDB.mdb")
            connString = provider
            myconnection.ConnectionString = connString

            If Convert.ToDateTime(mytxtboxdepart.Text) < Convert.ToDateTime(mytxtboxstays.Text) Then
                cell2.BackColor = Color.Red
                Dim str As String
                str = "DELETE * FROM ReservationTb WHERE Criteria" = "5"
            End If
            command.Connection = myAccessConnection
        End If
    End Sub

But i am getting the error ...
ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

Kamran MahmoodAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
You have myconnection defined, but the following line uses myAccessConnection
            command.Connection = myAccessConnection

Open in new window

Also, you are opening your connection and creating your SQL inside the IF, but then call the ExecuteNonQuery outside of the IF, which could cause issues.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
as what Shaun spotted, the code:


should be outside of if statement
Kamran MahmoodAuthor Commented:
Thanks Shaun for your help & support.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.