Error reading text files

Hello Experts,

I'm getting an error reading in text files into a listbox.  The problem is in the last line of the text file
which is ||>>>> * End * Of * JDS * File * <<<<||

If I just catch the error, it seems to work fine.  But I need to merge some of these files.  So I think I need to somehow remove that line.
But when I read it I get the error, if I catch it, it doesn't load that line.  What do I do?
BTW this is my first post
Jimmy LeaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DabasCommented:
Hi JimToGo:
Welcome to EE.
What is the exact error message?

Dabas
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Could you also show us how you are reading the file?

~IM
0
Jimmy LeaAuthor Commented:
This is the error:

System.IndexOutOfRangeException: Index was outside the bounds of the array.

From this function

Dim SR As StreamReader = Nothing
        Try
            SR = New StreamReader(fName)
            Dim record As String
            Dim Fields As String()
            Dim NewItem As ListViewItem
            Do While SR.Peek > -1
                record = SR.ReadLine
                Fields = record.Split(",")
                Dim K As Integer = (Fields(0).IndexOf(" - "))
                NewItem = New ListViewItem(Fields(0).Substring(K + 3))
                NewItem.SubItems.Add(Fields(2))
                NewItem.SubItems.Add(Fields(3))
                Dim I As Integer = (Fields(6).IndexOf(":"))
                NewItem.SubItems.Add(Fields(6).Substring(I + 2))
                JDSListView.Items.Add(NewItem)
            Loop
        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            SR.Close()
        End Try
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

DabasCommented:
In your Catch, have it also show you ex.StackTrace.
Also in your options, Text Editor Basic check the Line numbers option.

it will help to know which line is causing the problem

You can add an if statement after your split statement:

if Ubound(Fields) < 2 then
    'We know that it is the last line
0
Jimmy LeaAuthor Commented:
I tried that and still got the error

System.IndexOutOfRangeException: Index was outside the bounds of the array.
at JDSController.frmMain.GetJDSData(StringfName)

It seems to be in the do while sr.peek, I think when it reads that line it throws the exception.

Here is the last code I tried.

Sub GetJDSData(ByVal fName As String)
        Dim SR As StreamReader = Nothing
        Try
            SR = New StreamReader(fName)
            Dim record As String
            Dim Fields As String()
            Dim NewItem As ListViewItem
            Do While SR.Peek > -1
                record = SR.ReadLine()
                Fields = record.Split(",")
                If UBound(Fields) < 2 Then
                    Dim K As Integer = (Fields(0).IndexOf(" - "))
                    NewItem = New ListViewItem(Fields(0).Substring(K + 3))
                    NewItem.SubItems.Add(Fields(2))
                    NewItem.SubItems.Add(Fields(3))
                    Dim I As Integer = (Fields(6).IndexOf(":"))
                    NewItem.SubItems.Add(Fields(6).Substring(I + 2))
                    JDSListView.Items.Add(NewItem)
                End If
            Loop
        Catch ex As Exception
            MsgBox(ex.StackTrace.ToString)
        Finally
            SR.Close()
        End Try
    End Sub
0
DabasCommented:
JimToGo:
Negative. That would not give you an Index outside the bounds of the array exception.

Maybe the problem is another line?
Try to check that your project is configured to supply debug information.
The stack trace should give you the exact line number of the error.

Dabas
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jimmy LeaAuthor Commented:

This is the trace with line numbers.
 System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at JDSController.frmMain.GetJDSData(String fName) in C:\VB.Net Projects\JDSController\frmMain.vb:line 691

 Do While SR.Peek > -1
                record = SR.ReadLine
                Fields = record.Split(",")
                If UBound(Fields) < 2 Then
                    Dim K As Integer = (Fields(0).IndexOf(" - "))
                    NewItem = New ListViewItem(Fields(0).Substring(K + 3))
This is line 691                   NewItem.SubItems.Add(Fields(2))
                    NewItem.SubItems.Add(Fields(3))
                    Dim I As Integer = (Fields(6).IndexOf(":"))
                    NewItem.SubItems.Add(Fields(6).Substring(I + 2))
                    JDSListView.Items.Add(NewItem)
                End If
            Loop

If I edit the text file and take that line out, they load without exception.

Also the code above will not load the listbox.  But still throws the exception.  Do I have the if statement in the wrong place?
If I take the If statement out it will load the listbox and throw the exception.  
It seems like it should work with the if statement, because it's throwing the exception when it tries to split into multiple fields.

I appreciate your help.
0
Jimmy LeaAuthor Commented:
Got it.

You were right about the if statement I just didn't implement it correctly.
I was trying
 If UBound(Fields) < 2 Then
It should of been this:
If UBound(Fields) > 2 Then

it works fine now.  Thanks for your help
0
DabasCommented:
JimToGo:
Glad I could help!
Sometimes it is the simplest bug that eludes us! LOL

Dabas
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.