Hi, I am trying to delete data from dataGridview, and update my Access database, but I am having trouble withe these code it keep stated that I have "Object not set to an instance of an object" and I don't know where my code went wrong, please help, I am desperate...thanks you

Here are my code:

 Dim con As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=Individual.mdb")
    Dim btnDelete As Windows.Forms.DataGridViewButtonColumn
    Dim MaxRows As New Integer
    Dim da As New OleDb.OleDbDataAdapter
    Dim ds As New DataSet
    Dim inc As New Integer
Private Sub GridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridView1.CellContentClick

        Try
            If GridView1.Columns(e.ColumnIndex).Name = "Delete" AndAlso Me.GridView1.Rows(e.RowIndex).IsNewRow = False Then


                'i = GridView1.CurrentRow.Index
                'table.Rows(i).Delete()
                Dim cb As New OleDb.OleDbCommandBuilder(da)

                ds.Tables("AddressBook").Rows(inc).Delete()
                MaxRows = MaxRows - 1
                inc = 0
                da.Update(ds, "AddressBook")

                Me.GridView1.EndEdit()
                Me.GridView1.Rows.RemoveAt(e.RowIndex)
                da.Update(ds, "AddressBook")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
    End Sub

    Private Sub IndiAdd3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim btn As New DataGridViewButtonColumn()
        GridView1.Columns.Add(btn)
        btn.HeaderText = "Delete"
        btn.Text = "Delete"
        btn.Name = "Delete"
        btn.UseColumnTextForButtonValue = True
    End Sub

Thanks a lot.

Recommended Answers

All 5 Replies

Use a break point, and tell us where exactly the error occurs.

For deleting data with the help of datagridview and updating in access u can use

'Open connection
   Dim i As Integer
    Dim ID As String
    i = DatagridView1.CurrentRow.Index
    ID = DatagridView1.Item(0, i).Value 'whichever will be primarykey or unique data
      Try
        Dim MessageText As String
        Dim myCommand As OleDbCommand
        myCommand = New OleDbCommand("Delete from tablename where colname='" + ID + "'", Connection)
        Dim DBReader As OleDbDataReader = myCommand.ExecuteReader
        DBReader.Close()
      Catch ex As Exception
        MsgBox("Error in delete query: " + ex.Message)
      End Try
'then show the updated datagridview again

Hope this helps

Thanks guys, I will tell you the result after i tried all the solutions you gave.

'Open connection
Dim i As Integer
Dim ID As String
i = DatagridView1.CurrentRow.Index
ID = DatagridView1.Item(0, i).Value 'whichever will be primarykey or unique data
Try
Dim MessageText As String
Dim myCommand As OleDbCommand
myCommand = New OleDbCommand("Delete from tablename where colname='" + ID + "'", Connection)
Dim DBReader As OleDbDataReader = myCommand.ExecuteReader
DBReader.Close()
Catch ex As Exception
MsgBox("Error in delete query: " + ex.Message)
End Try
'then show the updated datagridview again

poojav, I tried ur code and get this error
Data type mismatch
my ID is in AutoNumber, and I cannot declare AutoNumber in VB.Net, what can I do?

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Dim dt As New DataTable
            Dim ds As New DataSet
            ds.Tables.Add(dt)
            Dim da As New OleDbDataAdapter

            con.Open()

            da = New OleDbDataAdapter("Select * from tblContacts", con)
            da.Fill(dt)

            dt.Rows(0).BeginEdit()
            dt.Rows(0).Delete()
            dt.Rows(0).EndEdit()
            'i = GridView1.CurrentRow.Index
            'table.Rows(i).Delete()
            Dim cb As New OleDbCommandBuilder(da)

            'ds.Tables("AddressBook").Rows(inc).Delete()
            'MaxRows = MaxRows - 1
            'inc = 0
            da.Update(dt)

            GridView1.DataSource = dt.DefaultView

            'Me.GridView1.EndEdit()
            'Me.GridView1.Rows.RemoveAt(e.RowIndex)



        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
        con.Close()
    End Sub

    Private Sub IndiAdd3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.Open()

        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter

        da = New OleDbDataAdapter("Select * from tblContacts", con)
        da.Fill(dt)

        GridView1.DataSource = dt.DefaultView

        con.Close()
    End Sub

This the way I solved the problem, get it from youtube. thanks for the guide guys.

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.