0

How can I include a condition to check a excel cell column value to see if it is empty or not? I've already mapped the column using linq mapping. When I go to grab the value whether it is null or not in the excel file, i'm getting object not set to reference error. Any help is appreciated. Here's what I got:

 public class Test
 {
 public string excelValue { get; set; }
 }

 string excel = @"C:\excel.xls";
 var excelFile = new ExcelQueryFactory(excel);
 excelFile.AddMapping<ExcelClass>(p => p.excelValue, "Name");
 var getvalue= excelFile.Worksheet<excelClass>("Sheet1").ToList();
 return getvalue;

public string GetMyName(List<excelClass> nameValue)
{
//NEED TO CHECK BEFORE SENDING TO A STRING VARIABLE!!!!!

string name = nameValue[1].excelValue.ToString();
return name;

}

1 Answers1

1

You can't call a method on a null object, whether it is ToString() or anythig else.

if(nameValue[1] != null)
{
   if(nameValue[1].excelValue != null)
   {
       return nameValue[1].excelValue.ToString();
   }
}
Crowcoder
  • 11,250
  • 3
  • 36
  • 45