ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 275.4k

There is now row at position 4 error get when try to remove

Aug 18 2018 6:09 PM
problem
 
There is now row at position 4 error get when try to remove unitcode exist on another datatble  

I have two data tables

dtwrongvaluesFinal

dtcheckrepeated

dtwrongvaluesFinal data as Following :

  1. UnitCode    CurrentReading  
  2. 122            50  
  3. 123            12  
  4. 7678           23  
  5. 1290           17  

dtcheckrepeated data show only data repeating on excel sheet as following

  1. UnitCode CurrentReading  
  2.  122           50   
  3. 122             12   
  4. 7678           23   
  5. 7678           17  

Actually i need to remove UnitCode that repeated in dtcheckrepeated from datatable dtwrongvaluesFinal

so that i write code below but problem it give me exception error there is no row at position 4 but problem i dont know

How to handle it

my code as below

  1. if (dtwrongvaluesFinal.Rows.Count > 0)  
  2. {  
  3.     for (int j = 0; j <= dtwrongvaluesFinal.Rows.Count-1; j++)  
  4.     {                                     
  5.         if (dtcheckrepeated.Rows.Count > 0)  
  6.         {                                    
  7.             for (int m = 0; m <= dtcheckrepeated.Rows.Count-1; m++)  
  8.             {  
  9.                 if (j <= dtcheckrepeated.Rows.Count - 1)  
  10.                 {  
  11.  UnitCodeWrongchk = Utilities.ObjectConverter.ConvertToString(dtcheckrepeated.Rows[j]["UnitCode"]);  
  12. UnitCodeWrongchkwrong = Utilities.ObjectConverter.ConvertToString(dtcheckrepeated.Rows[m]["UnitCode"]);  
  13.                 if (UnitCodeWrongchkwrong == UnitCodeWrongchk)  
  14.                 {
  15.                     dtwrongvaluesFinal.Rows.RemoveAt(m);  
  16.                 }  
  17.             }  
  18.         }  
  19.     }  
  20.     dtwrongvaluesFinal.AcceptChanges();  
  21.   }  
  22.  }  

The error show on line

UnitCodeWrongchk = Utilities.ObjectConverter.ConvertToString(dtcheckrepeated.Rows[j]["UnitCode"]);
 
dtwrongvaluesFinal.Rows.Count is 20 and
dtcheckrepeated.Rows.Count is 4
 
So that How to handle this error please ? 

Answers (3)