Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1553
  • Last Modified:

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

0
alsam
Asked:
alsam
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
Pratima PharandeCommented:
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

0
 
alsamAuthor Commented:
I tried but I get the same error...
Do you maybe have any other suggesstion....
0
 
jpaulinoCommented:
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Jacques Bourgeois (James Burger)PresidentCommented:
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

0
 
alsamAuthor Commented:
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....
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
First, a correction. This was typed late yesterday night and I did not do all the proper checks. The Rows collection starts at an index of 0. My loop should be:

For index As Integer = ds.Tables("dtsSP2").Rows.Count-1 To 0 Step -1

But this would not have caused the same error as the one you got.

Did you declare and fill your DataSet? Is there a table called "dtsSP2" in the DataSet? Are there Rows in that table?

Look for all those items in the debugger to see which one is not defined?

-----

alsam code cannot work, because you need a numeric index when you use the type of For that enables you to loop backward (n To n Step -n). He merged 2 different loops declarations into 1. He probably also wrote is answer too late at night.
0
 
jpaulinoCommented:
Sorry it was missing the .Count method :/
0
 
alsamAuthor Commented:
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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now