ASP.NET - VB.NET - How do I 1) Delete (remove) a row from a DataTable based on a value in the table and 2) How do I save the datatable changes back to the database?

I'm using a DataTable as a DataSource to my DataGrid.  Until a final SAVE button is clicked, I'm keeping all changes only in the DataTable (since they may hit Cancel instead -- meaning, I don't want to commit any of their changes until the end).  I've been doing fine with additions and updates, but I'm confused on how to handle deletes.  This is what I have at the moment:

---------------------
    Sub dgMeds_DeleteRow(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        'Turn off editing:
        dgMeds.EditItemIndex = -1

        'Get MedID:
        Dim iMedID As Integer
        iMedID = dgMeds.DataKeys(e.Item.ItemIndex)

        'Get DataTable from Session:
        Dim objDt As New DataTable
        If Not IsNothing(Session("tbl")) Then
            objDt = Session("tbl")
        End If

        'Delete the row with the specified medid from the DataTable in memory:
        Dim iLoop As Integer

        For iLoop = 0 To objDt.Rows.Count - 1
            If iMedID = objDt.Rows(iLoop)("medid").ToString() Then
                objDt.Rows(iLoop).Delete()
            End If
        Next iLoop

        'Save DataTable back to Session:
        Session("tbl") = objDt

        'Rebind meds grid:
        Me.RefreshMedsGridFromSession()

    End Sub
---------------------

I'm not sure if I'm even doing this right at this point.  And I've seen both Delete and also Remove for rows in a DataTable, and don't understand the difference.

I'm not getting any errors when I delete a row from my grid, using the above code, however I am getting an error when I go to ultimately save everything back to the real database:
------------------------
Deleted row information cannot be accessed through the row.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.DeletedRowInaccessibleException: Deleted row information cannot be accessed through the row.
Source Error:
Line 1386:
Line 1387:            For intI = 0 To objDt.Rows.Count - 1
Line 1388:                If objDt.Rows(intI)("medid") = 0 Then                                  <<< offending row
Line 1389:                    'NEW ROW WTIH MEDID=0 - INSERT NEW ROW:
Line 1390:                    Dim strSQL As String
-----------------------

So, what I'm gathering is that the row really isn't deleted from the DataTable, that it is just marked for deletion and that is why the error is occurring??  How can I cycle through these rows so that I can make the updates back to the database, and how can I reference a deleted row, so that I can truly delete it from the real db (in this same loop)?

Please let me know if there is more info that you need (additional code) so that my question is clear.  I didn't want to overwhelm you with unnecessary code.
thank you!!!!!!!!
LVL 9
sah18Asked:
Who is Participating?
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.

DBAduck - Ben MillerPrincipal ConsultantCommented:
You must check out this blog entry.

This is what you are looking for to access the data in the deleted rows, etc.

http://davidhayden.com/blog/dave/archive/2006/04/02/2899.aspx

Ben.
0

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
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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.