I have found many questions similar to this, but none that solve my issue. I am using SSIS and a C# script to read and modify the styling of an Excel workbook.
I get the following error "Cannot perform runtime binding on null reference". I understand what the error means; essentially you cannot use/reference something that is null. But I thought I was checking for all NULLs within my IF statement.
The code I'm using:
int rows = xlWorkSheetTY.UsedRange.Rows.Count - 1;
Excel.Range rge = xlWorkSheetTY.get_Range("J2:J" + rows, System.Type.Missing);
foreach (Excel.Range item in rge.Cells)
{
if (item != null && (item.Value2 != null || item.Value2 != "" || item.Value2 != "NULL"))
{
decimal result = (decimal)0.00;
if (decimal.TryParse(item.Value2.ToString(), out result))
{
if (result <= 20)
{
item.Interior.Color = Excel.XlRgbColor.rgbRed;
item.Font.Color = Excel.XlRgbColor.rgbWhite;
}
}
}
}
Anyone have any suggestions as to why I am getting this error and what I can do to rectify?
UPDATE:
I have inserted a try catch
statement to try and find some more details about why it was failing. It failed on a specific row every time. I have viewed this row in the raw Excel file and the data is empty.
How can I prevent it from parsing this field without doing what I already have - checking for nulls
ect?