Link to home
Start Free TrialLog in
Avatar of alsam
alsam

asked on

Deleted row information cannot be accessed through the row - Error massagge in VB code


Hello,
I have this problem which I don't know how to solve...Please see my VB code below....
The exception messagee is as follows:

"Deleted row information cannot be accessed through the row." and it breaks in line marked as "Here breaks..."....
It happens when I try recursevily to delete checked nodes in treeview from datatable...

I would appreciate If someone could help me to solve the problem...
Thanks in advance...
Private Sub oTV_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles oTV.AfterCheck

        If e.Node.ForeColor = Color.Blue Then GoTo Exitline 'Or e.Node.ForeColor = Color.Red Then GoTo Exitline
        CheckAllChildren(e.Node, e.Node.Checked)
Exitline:
        If e.Node.Checked = False Then
            UnCheckAllChildren(e.Node, e.Node.Checked)
        End If



    End Sub

Private Sub UnCheckAllChildren(ByVal tn As TreeNode, ByVal bCheck As [Boolean])
        bParentTrigger = True



        If tn.Nodes.Count = 0 Then


            For Each dr As DataRow In ds.Tables("dtSP2").Rows
                If tn.ForeColor = Color.Red Then GoTo next2
                If dr.RowState <> DataRowState.Deleted Then
                    If dr("Leaf_value") = tn.Tag Then
                        dr.Delete()
                    End If
                End If

next2:

            Next
        Else
         For Each ctn As TreeNode In tn.Nodes
                If ctn.Checked = True Then
                    bChildTrigger = True
                    bChildTrigger = False
                    For Each dr As DataRow In ds.Tables("dtSP2").Rows
                        If dr.RowState <> DataRowState.Added Then
                            If dr("Leaf_value") = tn.Tag Then ' <-----Here breaks error messagge "Deleted row information cannot be accessed through the row."
                                dr.Delete()
                            End If
                        End If
                    Next
                   ctn.Checked = bCheck
                End If


                CheckAllChildren(ctn, bCheck)
Next1:
            Next

         

        End If
        bParentTrigger = False
    End Sub

Open in new window

Avatar of Pratima
Pratima
Flag of India image

this error occours when u try to access the row which u have already deleted. So, try to check if the row exists or not and then retrieve the information.

Use Datatable.Acceptchanges() after you have deleted the row and  before you access a row.

try
ds.Tables("dtSP2").Acceptchanges()
afetr row deletion
refer
http://forums.asp.net/t/1278658.aspx

Avatar of alsam
alsam

ASKER

I tried but I get the same error...
Do you maybe have any other suggesstion....
When you delete rows you should always loop from the bottom to the top.

Change your For Each loop to For dr As DataRow = ds.Tables("dtSP2").Rows to 0 step -1 and correct the references in the loop
For many collections, using the more conventional For with a loop counter solves the problem as long as you loop backward:
For index As Integer = ds.Tables("dtsSP2").Rows.Count To 1 Step -1
   With  ds.Tables("dtsSP2").Rows(index)
                If tn.ForeColor = Color.Red Then GoTo next2
                If .RowState <> DataRowState.Deleted Then
                    If .Item("Leaf_value") = .Tag Then
                        .Delete()
                    End If
                End If
   End Witn
Next

Open in new window

Avatar of alsam

ASKER

Hi,
thanks for your replys
@JamesBurger
It gives me error "Object reference not set to an instance of an object."  in line For index As Integer = ds.Tables("dtsSP2").Rows.Count To 1 Step -1

@jpaulino
you proposed solution gives me error "'For' loop control variable cannot be of type 'System.Data.DataRow' because the type does not support the required operators."

I would appreciate a lot if you could give me some additional advice how to try to solve this....
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alsam

ASKER

Hi all,
thank you once more for your time and effort in order to help me solve the problem...
I spent most of the day yesturdey trying to find solution and finally I got it...
I reorganized my code and here is my final soultion (if you are interested ) which actually works perfectly...For me always have to be very complicated at the begining (unfortunatelly)....
Anyway once more I thank you....

PS: oTv_AfterCheck  can be used insted oTV_KeyDown event as in original post but anyway eventually does not metter...

regards...
Private Sub oTV_KeyDown(ByVal sender As Object, _
ByVal e As KeyEventArgs) Handles oTV.KeyDown
      
        If e.Control And e.KeyCode = Keys.E Then
            AddNodes()
            ' Check_All_Children(oTV.SelectedNode)
        ElseIf e.Control And e.KeyCode = Keys.D Then
            DeleteNodes()
        End If
    End Sub
    
    Private Sub AddNodes()
        oTV.BeginUpdate()
        If Not IsNothing(oTV.SelectedNode) Then
            Dim Values As TreeNodeCollection = Nothing
            FindLeaves(oTV.SelectedNode, Values)
        End If
        oTV.EndUpdate()
        oTV.Nodes(0).EnsureVisible()
    End Sub

    Private Sub FindLeaves(ByVal TN As TreeNode, ByVal leaves As TreeNodeCollection)
        If TN.Nodes.Count = 0 Then
            Check_All_Children(TN)
        Else
            For Each node As TreeNode In TN.Nodes
                If node.Nodes.Count = 0 Then
                    Check_All_Children(node)
                Else
                    FindLeaves(node, leaves)
                End If
            Next
        End If
    End Sub


    Private Sub Check_All_Children(ByVal tn As TreeNode) ', ByVal bCheck As [Boolean])

        If tn.ForeColor = Color.Blue Then GoTo Exitline
        '// Add new values into datatable//'
        Dim newfilterRow As DataRow = ds.Tables("dtSP2").NewRow()
        newfilterRow("FILTER_NAME") = ListView1.SelectedItems.Item(0).Text.ToString
        newfilterRow("FOLDER_NAME") = Me.ToolStripComboBox1.Text.ToString
        newfilterRow("LEAF_NAME") = Me.ToolStripComboBox2.Text.ToString
        newfilterRow("LEAF_VALUE") = tn.Tag.ToString
        newfilterRow("DESCRIPTION") = tn.Name.ToString
        ds.Tables("dtSP2").Rows.Add(newfilterRow)
        tn.ForeColor = Color.Red
        tn.EnsureVisible()
Exitline:
    End Sub
    


    Private Sub DeleteNodes()
        oTV.BeginUpdate()
        If Not IsNothing(oTV.SelectedNode) Then
            Dim Values As TreeNodeCollection = Nothing
            FindLeavesForDelete(oTV.SelectedNode, Values)
        End If
        oTV.EndUpdate()
        oTV.Nodes(0).EnsureVisible()
    End Sub

    Private Sub FindLeavesForDelete(ByVal TN As TreeNode, ByVal leaves As TreeNodeCollection)

        If TN.Nodes.Count = 0 Then
            If TN.ForeColor = Color.Blue Or TN.ForeColor = Color.Red Then
                DeleteChildren(TN)
                TN.ForeColor = Color.Black
            End If
        End If


        If TN.Nodes.Count <> 0 Then
            For Each node As TreeNode In TN.Nodes
                If node.Nodes.Count = 0 Then
                    If TN.ForeColor = Color.Blue Or TN.ForeColor = Color.Red Then
                        DeleteChildren(node)
                        node.ForeColor = Color.Black
                    End If
                End If
                FindLeavesForDelete(node, leaves)
            Next
        End If


    End Sub
    Private Sub DeleteChildren(ByVal tn As TreeNode) ', ByVal bCheck As [Boolean])
        Dim hierarchyrow() As Data.DataRow
        hierarchyrow = ds.Tables("dtSP2").Select("LEAF_VALUE = '" & tn.Tag & "' ")
        hierarchyrow(0).Delete()
    End Sub

Open in new window