0

I have the following code nested in a loop reading Excel Rows, it's supposed to read and house the current row's cell values.

IEnumerable<String> textValues = from cell in row.Descendants<Cell>()
//where cell.CellValue != null
select (cell.DataType.HasValue && cell.DataType == CellValues.SharedString 
          ? sharedString.ChildElements[int.Parse(cell.CellValue.InnerText)].InnerText 
          : cell.CellValue.InnerText
       );

Currently it's throwing null Exceptions. I would like to set them to an empty string, rather than skip them, so I still have something I can load into my object.

Panagiotis Kanavos
  • 90,087
  • 9
  • 138
  • 171
  • 4
    That's not LINQ to SQL, it's plain LINQ to Objects. Excel fields contain *text* and CellValue may simply be an empty string. Use String.IsNullOrWhitespace instead – Panagiotis Kanavos Feb 08 '16 at 14:37
  • Which statement is throwing? Where does `sharedString` come from? Checking the `DataType` doesn't mean the cell actually has a value. `cell.CellValue.InnerText` may be throwing, for example,or `ChildElements[int.Parse(cell.CellValue.InnerText)]` may be null, or .... – Panagiotis Kanavos Feb 08 '16 at 14:41
  • 2
    Please learn to use the debugger. If you'd already familiar with it you'd have mentioned _what_ is `null`. Possible duplicate: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Tim Schmelter Feb 08 '16 at 14:42
  • Thanks @PanagiotisKanavos, that was definitely my problem. It's still skipping the null value, I'd like it to insert an empty string or some kind of placeholder. Can you tell me how I should do that? Also, please enter your comment as an answer so I can vote on it ;)! – Nuv Xgibf Feb 08 '16 at 17:09

0 Answers0