deleting tables and rows from dataset asp.net 1.1

Hello experts I would just like some opinions on the most efficient way of deleting rows from a datatable and at the same time deleting a datatable from a dataset.

Basically i use a for each loop to loop all datatables in a dataset and also another loop to loop all rows in each datatable.
I check the row item values if they equal nothing I delete the row from the datatable I also want to check if the datatable has no rows delete it from the dataset.  I found the for each loop wont work because it changes the collection.  What is the best way to go about doing this?

  Dim shold, shold2 As String
            Dim shold3, shold4, shold5, shold6, shold7, shold8 As String()
       
 For Each dt As DataTable In main_dataset.Tables
                For Each dr In dt.Rows
                    shold = dt.Rows(x).Item(0)
                    shold2 = dt.Rows(x).Item(1)
                    shold3 = dt.Rows(x).Item(2).tostring.Split("-")
                    shold4 = dt.Rows(x).Item(3).tostring.Split("-")
                    shold5 = dt.Rows(x).Item(4).tostring.Split("-")
                    shold6 = dt.Rows(x).Item(5).tostring.Split("-")
                    shold7 = dt.Rows(x).Item(6).tostring.Split("-")
                    shold8 = dt.Rows(x).Item(7).tostring.Split("-")
                    If shold3(0) = " " And shold4(0) = " " And shold5(0) = " " And shold6(0) = " " And shold7(0) = " " And shold8(0) = " " Then
                        dt.Rows(x).Delete()
                    End If
                    x += 1
                Next
               main_dataset.Tables("dt").AcceptChanges()
                If dt.Rows.Count = 0 Then
                    main_dataset.Tables.Remove(dt)
                    main_dataset.AcceptChanges()
                End If
                x = 0
            Next
tentavariousAsked:
Who is Participating?
 
satish_nagdevConnect With a Mentor Commented:
hi,
could you please try following
                DataRow[] drs = ds.Tables[0].Select("field1 = ' ' and field2 = ' ' and ..."); //whatever you want t odo
      for(int Cnt=0; Cnt < drs.Length; Cnt++)
      {
            ds.Tables[0].Rows.Remove(drs[i]);
      }
      ds.Tables.Remove(indx/name); //i dont know other than this method to remove a table.

hope it helps.

regards,
satish
                  
0
 
tentavariousAuthor Commented:
I took out the for each loop and just used a for loop with counters how to I compensate for the change in the row and table counts so I dont get an out of index error

  For z As Integer = 0 To main_dataset.Tables.Count - 1
                For y As Integer = 0 To main_dataset.Tables(z).Rows.Count - 1
                    shold = main_dataset.Tables(z).Rows(y).Item(0)
                    shold2 = main_dataset.Tables(z).Rows(y).Item(1)
                    shold3 = main_dataset.Tables(z).Rows(y).Item(2).ToString.Split("-")
                    shold4 = main_dataset.Tables(z).Rows(y).Item(3).ToString.Split("-")
                    shold5 = main_dataset.Tables(z).Rows(y).Item(4).ToString.Split("-")
                    shold6 = main_dataset.Tables(z).Rows(y).Item(5).ToString.Split("-")
                    shold7 = main_dataset.Tables(z).Rows(y).Item(6).ToString.Split("-")
                    shold8 = main_dataset.Tables(z).Rows(y).Item(7).ToString.Split("-")
                    If shold3(0) = " " And shold4(0) = " " And shold5(0) = " " And shold6(0) = " " And shold7(0) = " " And shold8(0) = " " Then
                        main_dataset.Tables(z).Rows(y).Delete()
                        main_dataset.Tables(z).AcceptChanges()
                        If y <> 0 Then
                            y -= 1
                        End If
                    End If
                Next
                If main_dataset.Tables(z).Rows.Count = 0 Then
                    main_dataset.Tables.Remove(z)
                    main_dataset.AcceptChanges()
                    If z <> 0 Then
                        z -= 1
                    End If
                End If
            Next
0
 
nauman_ahmedCommented:
The foreach should work fine. Where are you getting the error?

--Nauman.
0
 
tentavariousAuthor Commented:
Foreach wont work if I try deleting the row I get the error: Collection was modified; enumeration operation may not execute,  the error occurs when I try to go to the next row after deleting a row.  My for loop above will delete the table rows during the loop but throws an item does not exist when it gets to one row after the last row in the datatable, since the table.rows.count is being changed during the loop, because of the deletion I dont know if its possible to delete within the for loop.
I think I got it working by adding two arrays to record the row and table numbers that need to be deleted but it seems unnecessary, there must be a better way.


 Dim rowhold(11) As Integer
            Dim tablehold(14) As Integer
            Dim shold, shold2 As String
            Dim shold3, shold4, shold5, shold6, shold7, shold8 As String()
            Dim x As Integer = 0
            Dim d As Integer = 0
            Dim p As Integer = 0
             Dim dr As DataRow
            For z As Integer = 0 To main_dataset.Tables.Count - 1
                For y As Integer = 0 To main_dataset.Tables(z).Rows.Count - 1
                    '  For y As Integer = 0 To main_dataset.Tables(z).Rows.Count - 1
                    shold = main_dataset.Tables(z).Rows(y).Item(0).ToString
                    shold2 = main_dataset.Tables(z).Rows(y).Item(1).ToString
                    shold3 = main_dataset.Tables(z).Rows(y).Item(2).ToString.Split("-")
                    shold4 = main_dataset.Tables(z).Rows(y).Item(3).ToString.Split("-")
                    shold5 = main_dataset.Tables(z).Rows(y).Item(4).ToString.Split("-")
                    shold6 = main_dataset.Tables(z).Rows(y).Item(5).ToString.Split("-")
                    shold7 = main_dataset.Tables(z).Rows(y).Item(6).ToString.Split("-")
                    shold8 = main_dataset.Tables(z).Rows(y).Item(7).ToString.Split("-")
                    If shold3(0) = " " And shold4(0) = " " And shold5(0) = " " And shold6(0) = " " And shold7(0) = " " And shold8(0) = " " Then
                        rowhold(d) = y
                        d += 1
                    End If
                Next
                For na As Integer = LBound(rowhold) To UBound(rowhold)
                    main_dataset.Tables(z).Rows(rowhold(na)).Delete()
                Next
                main_dataset.Tables(z).AcceptChanges()
                If main_dataset.Tables(z).Rows.Count = 0 Then
                    tablehold(p) = z
                End If
            Next
            For na As Integer = LBound(tablehold) To UBound(tablehold)
                main_dataset.Tables.Remove(tablehold(na))
            Next
            main_dataset.AcceptChanges()
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.