I have to delete the records in a ms access table using vb.net when i click on a button.

Recommended Answers

All 7 Replies

Dim con as new OledbConnection
con = new OledbConnection (constring)
con.open()

Dim cmd as new OledbCommand
cmd = new OledbCommand("Delete Query")
cmd.ExecuteNonQuery()

cmd.Dispose()
con.close()

I tried this code and it is throwing an exception that connection property has not been initialized!!!
giving error in this statement

cmd.ExecuteNonQuery()

Hi,
pls post your code

try this

Dim con as new OledbConnection
con = new OledbConnection (constring)
con.open()

Dim cmd as new OledbCommand
cmd = new OledbCommand("Delete Query",con)
cmd.ExecuteNonQuery()

cmd.Dispose()
con.close()

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Try
        oCmdOleDb = New OleDbCommand("Delete from table1")
        oConnOleDb = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database1.mdb")
        oConnOleDb.Open()



        If (oCmdOleDb.ExecuteNonQuery > 0) Then
            StatusStrip1.Items(0).Text = ("records deleted from the table")
        End If

    Catch ex As Exception
        MsgBox(ex.Message.ToString(), MsgBoxStyle.Information)



    Finally

        oConnOleDb.Close()
    End Try


End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Try
oConnOleDb = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database1.mdb")
oConnOleDb.Open()
oCmdOleDb = New OleDbCommand("Delete from table1",oConnOleDb)


If (oCmdOleDb.ExecuteNonQuery > 0) Then
StatusStrip1.Items(0).Text = ("records deleted from the table")
End If

Catch ex As Exception
MsgBox(ex.Message.ToString(), MsgBoxStyle.Information)

Finally
oConnOleDb.Close()
End Try

End Sub

Thanks a ton...I solved the problem

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.