0
votes

I am creating a for loop to check if the cells are empty for an excel sheet, the error message I am getting is

An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Unknown Module. Additional information: Cannot convert null to 'bool' because it is a non-nullable value type.

I tried converting to string as suggested in another thread, but I am unlucky with it.

I am using Microsoft Excel 12.0 Object Library.

Here is my code:

for (int i = 1; i < 55555; i++)
{
     if (xlWorkSheet.Cells[i, 1] = null)
     {
          //////Write data into the cell 
     }
     else
     {
          //// i++;
     }
}
2

2 Answers

3
votes

It should be xlWorkSheet.Cells[i, 1] == null the single = assigns the value to the cell.

1
votes
if (xlWorkSheet.Cells[i, 1] = null)
{
     //////Write data into the cell 
}
else
{
     //// i++;
}

it's not

xlWorkSheet.Cells[i, 1] = null

it should be

xlWorkSheet.Cells[i, 1] == null

= is an assignment operator while == is used for comparison.