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?

Posted on 2006-04-12
Last Modified: 2008-02-01
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
            End If
        Next iLoop

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

        'Rebind meds grid:

    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!!!!!!!!
Question by:sah18
    1 Comment
    LVL 24

    Accepted Solution

    You must check out this blog entry.

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


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now