0

I am getting an Indexing error on my code but I am very sure that there is data inside the location I am trying to print.

Below is the problem code.

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
    Dim seriesName As String = ComboBox2.SelectedItem.ToString()
    Dim seriesLocation As String = "excelFiles/seriesID.csv"
    Dim seriesData = File.ReadAllText(seriesLocation)
    Dim seriesRows = Split(seriesData, vbCrLf)
    For Each line In seriesRows
        Dim seriesItem = Split(line, ",")
        If seriesItem(1) = seriesName Then '<--- this is the error line
            TextBox2.Text += seriesItem(2) + ","
        End If
    Next
End Sub

I have used this code in a similar sub which works, and have accessed the file in an another but I just can not understand why this isn't working on this one.

I am trying to use the item inside a combo-box and compare it to data from a CSV stored in an array.

I have added some pictures below showing the error and what happens when I try to display the location.

Error when running normally

Difference in Printing Location "0" (top) and "1" (bottom)

The only difference is that I have changed seriesItem(1) to seriesItem(0)

Thanks for your help

Gareth Blair
  • 1
  • 1
  • 4
  • `If line.Contains(",") Then` Your image even shows that line = "". So, no comma. – LarsTech Mar 18 '16 at 15:41
  • Possible duplicate of [Why does my VB.NET array have extra values?](http://stackoverflow.com/questions/29840510/why-does-my-vb-net-array-have-extra-values) – GSerg Mar 18 '16 at 15:42
  • Your file location probably isn't where you think it is. Use a full path. – LarsTech Mar 18 '16 at 16:04
  • @LarsTech I have used that path twice before in the program and they work fine. The problem is in the for loop, I tried JaydipJ solution and while it worked it wasn't comparing the right location in the array. – Gareth Blair Mar 18 '16 at 16:29

1 Answers1

0

So upon working with the program and the CSV, it seems that the program will only compare something if it is located in location 0. When I moved things around in the CSV I managed to get the program working.

As the problem has now become something else I am going to close this question and post a new one.

Thank you all for replying and offering your help.

Gareth Blair
  • 1
  • 1
  • 4