0

I have a simple "bug", but it seems that I cannot figure it out. I'm reading an excel file (.xls) using ExcelLibrary, and I have a Do Loop, to go through my file. At the end of the file (my last row with data), the condition should be true and the loop should stop. Here is my issue: I get this error when the loop reaches my last row : "System.NullReferenceException: Object reference not set to an instance of an object."

Here is my code:

Try
    Do Until TryCast(WS.Cells(ws_Row, 2).Value.ToString, String) = Nothing

        ' Get data into a data table
        ws_Row = ws_Row + 1

    Loop
Catch ex As Exception
    LBL_ERR.Text = ex.ToString
    LBL_ERR.ForeColor = System.Drawing.Color.Red
    Exit Sub
End Try

This should be so simple to accomplish, yet I get this error message PS. The error I receive is at the row with the Do Until row

Marius T
  • 15
  • 4
  • PS. I've put the loop into a Try Catch, because this way I can keep the data stored in the DataTable, and continue the code. – Marius T Apr 15 '16 at 07:28

1 Answers1

0

Try this,

Do Until WS.Cells(ws_Row, 2).Value Is Nothing
INDIA IT TECH
  • 1,902
  • 4
  • 12
  • 25