I’m using a loop condition to check if there is null value in the columns, then remove it.
[STart:]
For i As Integer = counter To dt1.Columns.Count - 1
For x As Integer = 0 To dt1.Rows.Count - 1
if some condition then
something = true
else
something = false
counter = counter + 1
Goto [Start]
end if
Next
If something = true
dt1.Columns.Remove(dt1.Columns(i))
i -= 1
End If
Next
End If
Sometimes it runs correctly and sometimes even though when i becomes greater than (dt.columns.count - 1), it still executes the for loop and throws an error that there is no column with that index. I must be missing something here but I’m not able to debug the issue.
Do you guys find anything wrong with this code?
The statement
dt1.Columns.Countis evaluated at the very beginning of the for loop. It does not get re-evaluated each time the loop runs.So when you remove a column in the body of the loop it causes the end condition of the loop to go wrong.