How to read data from a CSV file and store the info in a table?


I have two columns in a CSV file and I need to store each column in a separate array.

Vagarth Gaurav,True
Dhananjay Menon,False

I would like to have one array for name, and another array for availability as a boolean value stored similar to below.

Name(0) = "Vagarth Gaurav"
Name(1) = "Dhananjay Menon"
Available(0) = True
Available(1) = False

The only problem is reading the CSV and storing the strings and booleans into the proper arrays.

Please help, I am new to VB. I am using Visual Basic 2010

I'm sure this question has been asked before, but this should help. Utilizing the TextFieldParser Class to do the parsing for you makes it easy. Code for managing the two arrays is displayed in this code example too.

    Dim arrName() As String
    Dim arrValue() As String

    Using ioReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\test\test.csv")

        ioReader.TextFieldType = FileIO.FieldType.Delimited

        While Not ioReader.EndOfData

            Dim arrCurrentRow As String() = ioReader.ReadFields()

            If arrName Is Nothing Then

                ReDim Preserve arrName(0)
                ReDim Preserve arrValue(0)

                arrName(0) = arrCurrentRow(0)
                arrValue(0) = arrCurrentRow(1)


                ReDim Preserve arrName(arrName.Length)
                ReDim Preserve arrValue(arrValue.Length)

                arrName((arrName.Length - 1)) = arrCurrentRow(0)
                arrValue((arrValue.Length - 1)) = arrCurrentRow(1)

            End If

        End While