i am importing data from excel to sqlserver..i have filled the dataset with the data in excel sheet..and now iam manipulating data within dataset like removing empty rows from sheet.I have written this code for eliminating empty rows but it is showing me an error that”there is no row at 18 position.” And also the count of empty rows is 11.This is the code snippet:
public DataSet GetExcelData()
{
DataTable ExcelTable = new DataTable();
List<int> rowToRemove = new List<int>();
//DataRow ExcelRow = new DataRow();
foreach (DataRow excelrow in dsExcel.Tables[0].Rows)
{
bool IsEmpty = false;
foreach (object item in excelrow.ItemArray)
{
if (String.IsNullOrEmpty(item.ToString()))
{
IsEmpty = true;
break;
}
else
{
IsEmpty = false;
}
}
if (IsEmpty)
{
**rowToRemove.Add((dsExcel.Tables[0].Rows.IndexOf(excelrow)));**
}
}
for (int i = 0; i < rowToRemove.Count; i++)
{
**dsExcel.Tables[0].Rows.RemoveAt(rowToRemove[i]);**
//dsExcel.Tables[0].Rows.Remove(rowNumber); ; ;
}
return dsExcel;
}
the lines which i have marked over there i am getting problem.it is not taking proper rows.
I have placed a breakpoint and also checked..it showed in this manner:
[0] 0
[1] 2
[2] 4
[3] 6
[4] 8
[5] 10
[6] 12
[7] 14
[8] 16
[9] 18
[10] 20
it is counting the rows from 0 position..which is not there at all.and it is not taking the empty row at 22 position..
please say me what i need to do here.
As soon as you removed the first item from the list all the following indexes are off.
You might want to remove the last one first: