Solved

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

Posted on 2011-09-28
8
1,336 Views
Last Modified: 2012-05-12

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
Comment
Question by:alsam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36813129
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
 
LVL 1

Author Comment

by:alsam
ID: 36813235
I tried but I get the same error...
Do you maybe have any other suggesstion....
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 36813280
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 40
ID: 36813323
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
 
LVL 1

Author Comment

by:alsam
ID: 36813515
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
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 36816089
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
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 250 total points
ID: 36816355
Sorry it was missing the .Count method :/
0
 
LVL 1

Author Comment

by:alsam
ID: 36890274
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

732 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