Parse csv files

Inboxxy

New member
Joined
Dec 5, 2010
Messages
3
Programming Experience
1-3
Hello all, im new here to please bear with me on this. Im pretty new to vb.net as well with little to almost no experience in vb syntax. Scripting i do alright with though.

Ive been googling all day and havent found anything helpful there or at bing/yahoo etc and my problem is this.

i have a csv file that has 3 columns from my database in it and it looks like this even with the blank row between them.
VB.NET:
"column1","column2","column3"

"column1","column2","column3"

"column1","column2","column3"


and my source looks like

VB.NET:
    Public Sub parser()
        SyncLock locker
            Dim accts As String
            Dim currentfield As String
            accts = TextBox1.Text
            Using myreader As New Microsoft.VisualBasic.FileIO.TextFieldParser(accts)
                myreader.TextFieldType = FileIO.FieldType.Delimited
                myreader.Delimiters = New String() {",", "\n"}
                Dim currentrow As String()
                While Not myreader.EndOfData
                    Try
                        currentrow = myreader.ReadFields()

                        For Each currentfield In currentrow
                            MsgBox(currentfield)
                        Next
                    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                        MsgBox("Line " & ex.Message & "invalid - skipped")
                    End Try
                End While
            End Using
        End SyncLock
    End Sub

This works fine an dandy for dumping each field, delimited by the comma, and it displays all the information in a text box. However, i need column 2 ONLY. Can anybody help me out? i also searched on here and the closest thing i found was
VB.NET:
http://www.vbdotnetforums.com/vb-net-general-discussion/42590-parsing-csv-file.html
but its still not quite what im after.

Thank you for reading :)
 
Well done for trying! So many people come here without even attempting before asking for help!

Bearing in mind that arrays are 0-based, try something like :-

VB.NET:
MessageBox.Show(currentrow(1))
 
See i tried doing that earlier but it dumps the 2nd letter after every delimited field

ex: "abc","def","ghi"

if i use
VB.NET:
msgbox(currentrow(1))

itll return the 2nd letter after every delimited field, which in this case would be
a
d
g


already ahead of you on it but i appreciate the help :)


edit#2: also ive tried used 3rd party libraries as well and they all come up short.
 
Last edited:
I wrote a CSV file with the following contents :-

VB.NET:
"column11","column12","column13"
"column21","column22","column23"
"column31","column32","column33"

and then used :-

VB.NET:
        Using myreader As New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\temp.csv")
            myreader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            myreader.Delimiters = New String() {",", "\n"}
            Dim currentrow As String()
            While Not myreader.EndOfData
                Try
                    currentrow = myreader.ReadFields()
                    MessageBox.Show(currentrow(1))

                    'For Each currentfield In currentrow
                    'MessageBox.Show(currentfield)
                    'Next
                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    MessageBox.Show("Line " & ex.Message & "invalid - skipped")
                End Try
            End While
        End Using
        MessageBox.Show("ok")

and it gave me 3 messageboxes, with column12, then column22, then column32. I suggest you try again :)
 
OMG WOW

i was doing

msgbox(currentfield(2)) not msgbox(currentrow(2))

it never occured to me to do that. holy wow.

thank you so so so very much. Dont you hate it when something so simple is right in front of you and you dont even realize it?

whats worse is i didnt fully read your first response above well enough i guess.
 
Last edited:
Dim s as String = "abcdef"

s(3) 'returns the character "d"


-> Strings are treatable as arrays of characters, just like your array of strings from parisng the text file.. i.e. CURRENTROW is an array of strings, a string is an array of characters. CURRENTROW(2) gets the third string in the array. CURRENTROW(2)(2) gets the third character of the third string..

It was a simple mistake :)
 
Back
Top