error when delete a duplicate value from datatable Error: there is no row at position asp.net vb.net

HOUSSEM MAHJOUBI 286 Reputation points
2020-12-03T10:19:28.167+00:00

HI members
i want to remove duplicated value from datatable
but when i execute it return Error: there is no row at position
a 1 10

b 2 20

c 3 30

  3      0

  4       0

i want if there no value in first column begin test from second column if duplicate should delete the row that has no value in 1st column
if 1st column has no value test if 2nd column has a duplicated value in all table then will deleted
3 0 will be deleted
this my code

Dim conter As Integer = tab10.Rows.Count - 1 //this the top datatable
Dim conter2 As Integer = tab.Rows.Count - 1 // this is the the bottom with the 1st column is empty

    For i = 0 To conter



        For j = 0 To conter2


            If tab10.Rows(i).Item(2) = tab.Rows(j).Item(2) Then

                tab10.Rows(i).Delete()

                conter = tab10.Rows.Count - 1
                Exit For
            End If
        Next



    Next

i wish this clear enough

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,507 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,086 Reputation points
    2020-12-04T08:28:00.1+00:00

    Hi @HOUSSEM MAHJOUBI ,

    As far as I think,you could replace for with for each.Just like this:

      Dim rowsToDelete As List(Of DataRow) = New List(Of DataRow)()  
        For Each dr10 As DataRow In tab10.Rows  
    
            For Each dr As DataRow In tab.Rows  
                If dr10.Item(2) = dr.Item(2) Then  
                    rowsToDelete.Add(dr10)  
                End If  
            Next  
        Next  
        For Each r In rowsToDelete  
            tab10.Rows.Remove(r)  
        Next  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Yijing Sun


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.