Question

I am reading a Excel file with 60 columns. Problem is if the cell is empty it just throws index out of range exception and if cell contains some data then it reads the value. Any idea how can I read the empty cell. I am using the following code to do so. It works fine for empty cells for other types of data but these cells contain date values.

List<string> depDates = new List<string>();
        for (int j = 54; j < 58; j++)
        {
            if (tableData.DataRows[0][j] == string.Empty)
            {
                depDates.Add("null");
            }
            else
            {
                depDates.Add(tableData.DataRows[0][j]);
            }
        }

The format of these columns in Excel is Text. Please guide.

Was it helpful?

Solution

You can try this :

  if (tableData.DataRows[0][j] == null ||tableData.DataRows[0][j].Trim() == string.Empty)
        {
            depDates.Add("null");
        }

OTHER TIPS

Possibly there are whitespaces in cells, check cells like following:

if (string.IsNullOrWhitespace(tableData.DataRows[0][j])) ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top