Link to home
Start Free TrialLog in
Avatar of Kamran Mahmood
Kamran Mahmood

asked on

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
                myconnection.Open()
                Response.Write(Convert.ToDateTime(mytxtboxdepart.Text))
                cell2.BackColor = Color.Red
                Dim str As String
                str = "DELETE * FROM ReservationTb WHERE Criteria" = "5"
            End If
            command.Connection = myAccessConnection
            command.ExecuteNonQuery()
            myAccessConnection.Close()
        End If
    End Sub

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

PLS HELPPPPPP !!! EXPERTS
ASKER CERTIFIED SOLUTION
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
as what Shaun spotted, the code:

myconnection.Open()

should be outside of if statement
Avatar of Kamran Mahmood
Kamran Mahmood

ASKER

Thanks Shaun for your help & support.