?
Solved

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
3
Medium Priority
?
202 Views
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
                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!!!!!!!!
0
Comment
Question by:sah18
1 Comment
 
LVL 25

Accepted Solution

by:
DBAduck - Ben Miller earned 2000 total points
ID: 16441727
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 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