1

I am trying to delete selected row from datagridview and commit changes permanently in my access DB here is my code:

     cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Accountcode.accdb;"
    cn.Open()

    Try
        For Each row As DataGridViewRow In DataGridView1.SelectedRows
            Using cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Accountcode.accdb;")
                Using cm As New OleDb.OleDbCommand
                    cm.Connection = cn
                    cm.CommandText = "DELETE * FROM CheckDJ WHERE [ID]= @id"
                    cm.CommandType = CommandType.Text
                    cm.Parameters.AddWithValue("@ID", CType(row.DataBoundItem, DataRowView).Row("ID"))
                    cm.ExecuteNonQuery()
                End Using
            End Using
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    Try
        Using cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Accountcode.accdb;")
            Using cm As New OleDb.OleDbCommand
                strsql = "SELECT * FROM CheckDJ"
                cm.Connection = cn
                cm.CommandText = strsql
                cm.CommandType = CommandType.Text

                Dim da As New OleDbDataAdapter(strsql, cn)
                Dim ds As New DataSet()
                da.Fill(ds, "CheckDJ")
                DataGridView1.DataSource = ds.Tables(0)
            End Using
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

But I'm having an error.
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Any help is very much appreciated. Thank you in advance!

3
  • It's because you are removing rows while iterating them. Throw them in a collection first, then loop through each one and remove it from the datagridview and then remove it from the database. Currently your in a loop to remove records from datagridview but you are requiring the database and filling the datagridview again inside this loop, that is bad practice and asking for problems... Also use parameters and look into properly disposing connection objects, commands etc...Open connection do your work and the close and cleanup. Commented Jul 20, 2016 at 3:30
  • @Zaggler I would appreciate it very much if you can help me by showing me the correct code. You can modify my code. Commented Jul 20, 2016 at 3:38
  • @F0r3v3r-A-N00b see my edited code above Commented Jul 20, 2016 at 8:31

2 Answers 2

1
Try
        For Each row As DataGridViewRow In DatagridView1.SelectedRows
            Using cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Accountcode.accdb;")
                Using cm As New OleDb.OleDbCommand
                    cm.Connection = cn
                    cm.CommandText = "DELETE * FROM CheckDJ WHERE [ID]= @id"
                    cm.CommandType = CommandType.Text
                cm.Parameters.AddWithValue("@id", ctype(row.DataBoundItem,DataRowView).Row("id"))
                    cm.ExecuteNonQuery()
                End Using
            End Using
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    Try
        Using cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Accountcode.accdb;")
            Using cm As New OleDb.OleDbCommand
                    strsql = "SELECT * FROM CheckDJ"
                    cm.Connection = cn
                    cm.CommandText = strsql
                    cm.CommandType = CommandType.Text

        Dim da As New OleDbDataAdapter(strsql, cn)
        Dim ds As New DataSet()
        da.Fill(ds, "CheckDJ")
        DataGridView1.DataSource = ds.Tables(0)
           End Using
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
Sign up to request clarification or add additional context in comments.

3 Comments

@F0r3v3r-A-N00b : Object reference not set to an instance of an object. I got this error trying your code.
@F0r3v3r-A-N00b This line: cm.Parameters.AddWithValue("@id", ctype(row.DataBoundItem,DataRowView).Row("id"))
change the "id" in Row("id") to the actual name of your column
0

You're going about that all wrong. Firstly, use a data adapter to populate a DataTable and bind that to the grid. You would then get the bound DataRowView from each selected row in the grid, call its Delete method to mark it as Deleted (which will remove it from the grid) and then use the same data adapter to save the changes back to the database. E.g.

myDataAdapter.Fill(myDataTable)
myDataGridView.DataSource = myDataTable

'...

Dim rowsToDelete As New List(Of DataRowView)

For Each gridRow As DataGridViewRow In myDataGridView.SelectedRows
    rowsToDelete.Add(DirectCast(gridRow.DataBoundItem, DataRowView))
Next

For Each row In rowsToDelete
    row.Delete()
Next

myDataAdapter.Update(myDataTable)

1 Comment

I wondered about the DV myself. I do wish such people would leave a comment. If there's something that needs changing then we're not going to change it if we don't know what it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.