Link to home
Start Free TrialLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

C# using isNullorEmpty is not working

I am reading an Excel spreadsheet and am getting a message "Cannot perform binding on a null reference" I have discovered this is being caused by a blank or null in column 12 in one of the records in the spreadsheet. Looking at my code for delivery days(12) I thought this would catch this but it does not. How do I correctly handle this?

                for (int i = 1; i <= lastUsedRow; i++)
                {
                    if (i > 1)                   /* Ignore the first row as it contains column headings */
                    {
                        for (int j = 1; j <= lastUsedColumn; j++)
                        {
                            switch (j)
                            {
                                case 2:
                                    if (string.IsNullOrEmpty(xlRange.Cells[i, j].Value2.ToString()))
                                        custNmbr = string.Empty;
                                    else
                                        custNmbr = xlRange.Cells[i, j].Value2.ToString();
                                    break;
                                case 5:
                                    if (string.IsNullOrEmpty(xlRange.Cells[i, j].Value2.ToString()))
                                        adrsCode = string.Empty;
                                    else
                                        adrsCode = xlRange.Cells[i, j].Value2.ToString();
                                    break;
                                case 12:
                                    if (string.IsNullOrEmpty(xlRange.Cells[i, j].Value2.ToString()))
                                    {
                                        deliveryDays = string.Empty;
                                    }
                                    else
                                        deliveryDays = xlRange.Cells[i, j].Value2.ToString();
                                    break;
                                default:
                                    break;
                            }
                        }

                        /* Update the delivery days in customer master address table */
                        if (!String.IsNullOrEmpty(custNmbr) && !String.IsNullOrEmpty(adrsCode) && !String.IsNullOrEmpty(deliveryDays))
                            UpdateDeliveryDaysCustAddr(custNmbr, adrsCode, deliveryDays);      
                    }
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwheeler23

ASKER

Bingo. That fixed it.Thanks.