0

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.

0

2 Answers 2

1

You can try this :

  if (tableData.DataRows[0][j] == null ||tableData.DataRows[0][j].Trim() == string.Empty)
        {
            depDates.Add("null");
        }
Sign up to request clarification or add additional context in comments.

3 Comments

what about white spaces? Any case string.IsNullOrEmpty() and string.IsNullOrWhitespace() are designed for this purpose.
I think if there is white space it will be different of null.
exactly, it will not be null and it will not be string.Empty. you'll end up adding something like " " to collection of depDates. and that is why I've used string.IsNullOrWhitespace to answer this question
1

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

if (string.IsNullOrWhitespace(tableData.DataRows[0][j])) ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.