petrikor
petrikor

Reputation: 19

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in vb.net CSV program

This program is simply reading from a CSV and putting that into a DatagridView table. I am not sure what is causing the error shown below.

"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Minecraft Server Program.exe"

This error points to the line "newrow("Days_remaining") = columns(1)

Also, the CSV is formatted as such:

xX_EpicGamer_Xx,2,2/05/2021,9,6,Player was mean to others and stole lots of diamonds pewdiepie,3,2/05/2021,4,2,Player swore

Thank you for any help

Public Sub LoadCSVFile()

        'If the Input file cannot be found, then give error message And exit this Subroutine. 
        If Not File.Exists(BansCSVFile) Then
            MsgBox("Input CSV File of Item Prices not found: " & BansCSVFile)
            Exit Sub
        End If

        'Set up streamreader and variables for reading through file
        Dim srdCSV As New IO.StreamReader("bans.csv", System.Text.Encoding.Default)
        Dim sline As String = ""
        Do
            'Read through each line, separating with the comma and putting each entry into its respective column
            sline = srdCSV.ReadLine
            If sline Is Nothing Then Exit Do
            Dim columns() As String = sline.Split(",")
            Dim newrow As DataRow = datatable1.NewRow
            newrow("Player") = columns(0)
            newrow("Days_remaining") = columns(1)
            newrow("Date_started") = columns(2)
            newrow("Timespan") = columns(3)
            newrow("Rule_broken") = columns(4)
            newrow("Extra_info") = columns(5)
            datatable1.Rows.Add(newrow)
        Loop
        srdCSV.Close()

        dgvBans.DataSource = datatable1
        Me.Text = datatable1.Rows.Count & "rows"

    End Sub

Edit: This new code fixed the issue.

  Public Sub LoadCSVFile()

        'If the Input file cannot be found, then give error message And exit this Subroutine. 
        If Not File.Exists(BansCSVFile) Then
            MsgBox("Input CSV File of Item Prices not found: " & BansCSVFile)
            Exit Sub
        End If

        'Set up streamreader and variables for reading through file
        srdCSV = New IO.StreamReader("bans.csv")

        Dim totalrec As Integer = 0

        Try
            Do Until srdCSV.Peek = -1

                'Read through each line, separating with the comma and putting each entry into its respective column
                strInputFileLine = srdCSV.ReadLine()
                'Split CSV lines where commas are
                Dim RecordLineColumns() As String = strInputFileLine.Split(",")
                'Create a new row in data table and place data into it from array
                Dim newrow As DataRow = datatable1.NewRow
                newrow("Player") = RecordLineColumns(0)
                newrow("Days_remaining") = RecordLineColumns(1)
                newrow("Date_started") = RecordLineColumns(2)
                newrow("Timespan") = RecordLineColumns(3)
                newrow("Rule_broken") = RecordLineColumns(4)
                newrow("Extra_info") = RecordLineColumns(5)
                datatable1.Rows.Add(newrow)

                totalrec = totalrec + 1

            Loop



        Catch ex As Exception
            MsgBox("Error reading file")
            Exit Sub
        End Try
        srdCSV.Close()


        'Me.Text = datatable1.Rows.Count & "rows"

        MsgBox("Total input records = " & totalrec)


    End Sub

Upvotes: 0

Views: 95

Answers (2)

Mary
Mary

Reputation: 15091

I am guessing that the Extra_Info is not necessarily present for every record. I am checking for a length less 5 and exiting but if it has at least 5 we continue but only fill in the last field if the length is 6.

Public Sub LoadCSVFile(BansCSVFile As String)
    Dim datatable1 As New DataTable
    With datatable1.Columns
        .Add("Player")
        .Add("Days_remaing")
        .Add("Date_started")
        .Add("Timespan")
        .Add("Rule_broken")
        .Add("Extra_Info")
    End With
    If Not File.Exists(BansCSVFile) Then
        MsgBox("Input CSV File of Item Prices not found: " & BansCSVFile)
        Exit Sub
    End If
    Dim lines = File.ReadAllLines(BansCSVFile)
    For Each sline In lines
        Dim columns() As String = sline.Split(","c)
        If columns.Length < 5 Then
            MessageBox.Show($"Incomplete data on line {sline}")
            Return
        End If
        Dim newrow As DataRow = datatable1.NewRow
        newrow("Player") = columns(0)
        newrow("Days_remaining") = columns(1)
        newrow("Date_started") = columns(2)
        newrow("Timespan") = columns(3)
        newrow("Rule_broken") = columns(4)
        If columns.Length = 6 Then
            newrow("Extra_info") = columns(5)
        End If
        datatable1.Rows.Add(newrow)
    Next
    dgvBans.DataSource = datatable1
    Me.Text = datatable1.Rows.Count & "rows"
End Sub

Upvotes: 0

Think2826
Think2826

Reputation: 211

using the Try/Catch syntax

After checking which column has a problem, try verifying the csv file.

Upvotes: 1

Related Questions