Home All Groups Group Topic Archive Search About

CSV file reading every other record...

Author
16 May 2006 7:40 PM
bbepristis
Hey all I have a wired issue I have a csv file with , seperated values
I have some code to read the file and put the record into an array then
into textboxes. the problem is it seems to read everyother line of the
file not everyline at first I thought it might be the code then the csv
file The csv is a mysql generated via webmin however I cant seem to
find the problem anywhere so please advise.

-- CODE--
Public Sub csv_conn()
        'Opens CSV File reads contents Writes them to an Array then
Sets values to textboxes
        Call save()
        Dim line_num As Integer
        Dim psrdCurrent As System.IO.StreamReader
        Dim psrdline As String
        Dim pstrfields() As String
        Dim filename As String
        filename = "READ FILENAME"
        psrdCurrent = New System.IO.StreamReader(filename) ' OPen File
        psrdline = psrdCurrent.ReadLine()
        Do Until line_num = currecord
            Dim psrdfile As String
            psrdfile = psrdline & CrLf
            psrdline = psrdCurrent.ReadLine()
            Dim currentlinevalue As String
            currentlinevalue = psrdCurrent.ReadLine()
            pstrfields = Split(psrdline, ",") ' Parses document into an
Array with a , seperated value.
            companytxt.Text = pstrfields(1) ' Setting Text to Textboxes
            Nametxt.Text = pstrfields(2)
            addrtxt.Text = pstrfields(3)
            citytxt.Text = pstrfields(4)
            statetxt.Text = pstrfields(5)
            ziptxt.Text = pstrfields(6)
            countrytxt.Text = pstrfields(7)
            phonetxt.Text = pstrfields(8)
            faxtxt.Text = pstrfields(9)
            celltxt.Text = pstrfields(10)
            titletxt.Text = pstrfields(11)
            assistanttxt.Text = pstrfields(12)
            urltxt.Text = pstrfields(13)
            altcontacttxt.Text = pstrfields(14)
            altphonetxt.Text = pstrfields(15)
            altaddrtxt.Text = pstrfields(16)
            altcitytxt.Text = pstrfields(17)
            altstatetxt.Text = pstrfields(18)
            altziptxt.Text = pstrfields(19)
            altcountrytxt.Text = pstrfields(20)
            user1txt.Text = pstrfields(21)
            user2txt.Text = pstrfields(22)
            user3txt.Text = pstrfields(23)
            user4txt.Text = pstrfields(24)
            user5txt.Text = pstrfields(25)
            user6txt.Text = pstrfields(26)
            user7txt.Text = pstrfields(27)
            user8txt.Text = pstrfields(28)
            user9txt.Text = pstrfields(29)
            user10txt.Text = pstrfields(30)
            user11txt.Text = pstrfields(31)
            user12txt.Text = pstrfields(32)
            user13txt.Text = pstrfields(33)
            user14txt.Text = pstrfields(34)
            user15txt.Text = pstrfields(35)
            territorytxt.Text = pstrfields(26)
            recordcounttxt.Text = pstrfields(0)
            line_num += 1
        Loop
        psrdCurrent.Close()  ' Closes File
    End Sub
    Public Sub save()
        Dim line_num As Integer
        Dim filename2 As String
        Dim csv_text As String
        csv_text = recordcounttxt.Text & "," & companytxt.Text & "," &
Nametxt.Text & "," & addrtxt.Text & "," & citytxt.Text & "," &
statetxt.Text & "," & ziptxt.Text & "," & countrytxt.Text & "," &
phonetxt.Text & "," & faxtxt.Text & "," & celltxt.Text & "," &
titletxt.Text & "," & assistanttxt.Text & "," & urltxt.Text & "," &
altcontacttxt.Text & "," & altphonetxt.Text & "," & altaddrtxt.Text &
"," & altcitytxt.Text & "," & altstatetxt.Text & "," & altziptxt.Text &
"," & territorytxt.Text & "," & mech1serialnum.Text & "," &
mech1vacpump.Text & "," & mech1spindle.Text & "," &
mech1vacpumpserialnum.Text & "," & mech1filterpartnum.Text & "," &
mech2serialnum.Text & "," & mech2vacpump.Text & "," & mech2spindle.Text
& "," & mech2vacpumpserialnum.Text & "," & mech2filterpartnum.Text &
"," & mech3serialnum.Text & "," & mech3vacpump.Text & "," &
mech3spindle.Text & "," & mech3vacpumpserialnum.Text & "," &
mech3filterpartnum.Text & "," & mech4serialnum.Text & "," &
mech4vacpump.Text & "," & mech4spindle.Text & "," &
mech4vacpumpserialnum.Text & "," & mech4filterpartnum.Text & "," &
user1txt.Text & "," & user2txt.Text & "," & user3txt.Text & "," &
user4txt.Text & "," & user5txt.Text & "," & user6txt.Text & "," &
user7txt.Text & "," & user8txt.Text & "," & user9txt.Text & "," &
user10txt.Text & "," & user11txt.Text & "," & user12txt.Text & "," &
user13txt.Text & "," & user14txt.Text & "," & user15txt.Text
        filename2 = "WRITE FILENAME"
        Dim filename As String
        filename = "READ FILENAME"
        ' Create an empty string array to return to caller.
        Dim lines() As String = {}

        ' Check to see if the file exists.
        If File.Exists(filename) Then
            ' Open a stream reader to get the text from the file.
            Dim sr As New StreamReader(filename)
            ' Read all the file text.
            Dim fileText As String = sr.ReadToEnd()
            ' Close the stream reader
            sr.Close()

            ' Split the text into a string array delimited by Carriage
Return/Line Feed.
            lines = Split(fileText, vbCrLf)

            ' Return
            Dim fs As New FileStream(filename2, FileMode.Create)
            Dim sw As New StreamWriter(fs)

            Dim foundBlank As Boolean
            For Each line As String In lines
                If line.Length > 0 Then
                    line_num += 1
                    If line_num = currecord Then
                        sw.WriteLine(csv_text)
                        ' Reset blank line flag
                        foundBlank = False
                    Else
                        sw.WriteLine(line)
                        ' Reset blank line flag
                        foundBlank = False
                    End If
                Else
                    If Not foundBlank Then
                        ' Blank line: write first one
                        sw.WriteLine()
                        ' Set flag to indicate that blank line was
found
                        foundBlank = True
                    End If
                End If
            Next
            sw.Close()
            fs.Close()
        End If
        If System.IO.File.Exists(filename2) = True Then
            If System.IO.File.Exists(filename) = True Then
                System.IO.File.Delete(filename)
            End If
            System.IO.File.Copy(filename2, filename)
        End If
    End Sub



-- END CODE --
--CSV format--
client_id,company,Name,addr,city,state,zip,country,phone,fax,cell,title,assistant,url,altcontact,altphone,altaddr,altcity,altstate,altzip,territory,mech1serialnum,mech1vacpump,mech1spindle,mech1vacpumpserialnum,mech1filterpartnum,mech2serialnum,mech2vacpump,mech2spindle,mech2vacpumpserialnum,mech2filterpartnum,mech3serialnum,mech3vacpump,mech3spindle,mech3vacpumpserialnum,mech3filterpartnum,mech4serialnum,mech4vacpump,mech4spindle,mech4vacpumpserialnum,mech4filterpartnum,user1,user2,user3,user4,user5,user6,user7,user8,user9,user10,user11,user12,user13,user14,user15
--end csv format--

Author
17 May 2006 8:29 AM
Cor Ligthert [MVP]
BBepristis,

Just read the file in with
mystring = reader.readtoend
than split that with
dim mystringarray as string() = mystring.split(" "c)

I hope this helps,

Cor

Show quoteHide quote
"bbepristis" <brian.bepris***@gmail.com> schreef in bericht
news:1147808453.203115.297260@v46g2000cwv.googlegroups.com...
> Hey all I have a wired issue I have a csv file with , seperated values
> I have some code to read the file and put the record into an array then
> into textboxes. the problem is it seems to read everyother line of the
> file not everyline at first I thought it might be the code then the csv
> file The csv is a mysql generated via webmin however I cant seem to
> find the problem anywhere so please advise.
>
> -- CODE--
> Public Sub csv_conn()
>        'Opens CSV File reads contents Writes them to an Array then
> Sets values to textboxes
>        Call save()
>        Dim line_num As Integer
>        Dim psrdCurrent As System.IO.StreamReader
>        Dim psrdline As String
>        Dim pstrfields() As String
>        Dim filename As String
>        filename = "READ FILENAME"
>        psrdCurrent = New System.IO.StreamReader(filename) ' OPen File
>        psrdline = psrdCurrent.ReadLine()
>        Do Until line_num = currecord
>            Dim psrdfile As String
>            psrdfile = psrdline & CrLf
>            psrdline = psrdCurrent.ReadLine()
>            Dim currentlinevalue As String
>            currentlinevalue = psrdCurrent.ReadLine()
>            pstrfields = Split(psrdline, ",") ' Parses document into an
> Array with a , seperated value.
>            companytxt.Text = pstrfields(1) ' Setting Text to Textboxes
>            Nametxt.Text = pstrfields(2)
>            addrtxt.Text = pstrfields(3)
>            citytxt.Text = pstrfields(4)
>            statetxt.Text = pstrfields(5)
>            ziptxt.Text = pstrfields(6)
>            countrytxt.Text = pstrfields(7)
>            phonetxt.Text = pstrfields(8)
>            faxtxt.Text = pstrfields(9)
>            celltxt.Text = pstrfields(10)
>            titletxt.Text = pstrfields(11)
>            assistanttxt.Text = pstrfields(12)
>            urltxt.Text = pstrfields(13)
>            altcontacttxt.Text = pstrfields(14)
>            altphonetxt.Text = pstrfields(15)
>            altaddrtxt.Text = pstrfields(16)
>            altcitytxt.Text = pstrfields(17)
>            altstatetxt.Text = pstrfields(18)
>            altziptxt.Text = pstrfields(19)
>            altcountrytxt.Text = pstrfields(20)
>            user1txt.Text = pstrfields(21)
>            user2txt.Text = pstrfields(22)
>            user3txt.Text = pstrfields(23)
>            user4txt.Text = pstrfields(24)
>            user5txt.Text = pstrfields(25)
>            user6txt.Text = pstrfields(26)
>            user7txt.Text = pstrfields(27)
>            user8txt.Text = pstrfields(28)
>            user9txt.Text = pstrfields(29)
>            user10txt.Text = pstrfields(30)
>            user11txt.Text = pstrfields(31)
>            user12txt.Text = pstrfields(32)
>            user13txt.Text = pstrfields(33)
>            user14txt.Text = pstrfields(34)
>            user15txt.Text = pstrfields(35)
>            territorytxt.Text = pstrfields(26)
>            recordcounttxt.Text = pstrfields(0)
>            line_num += 1
>        Loop
>        psrdCurrent.Close()  ' Closes File
>    End Sub
>    Public Sub save()
>        Dim line_num As Integer
>        Dim filename2 As String
>        Dim csv_text As String
>        csv_text = recordcounttxt.Text & "," & companytxt.Text & "," &
> Nametxt.Text & "," & addrtxt.Text & "," & citytxt.Text & "," &
> statetxt.Text & "," & ziptxt.Text & "," & countrytxt.Text & "," &
> phonetxt.Text & "," & faxtxt.Text & "," & celltxt.Text & "," &
> titletxt.Text & "," & assistanttxt.Text & "," & urltxt.Text & "," &
> altcontacttxt.Text & "," & altphonetxt.Text & "," & altaddrtxt.Text &
> "," & altcitytxt.Text & "," & altstatetxt.Text & "," & altziptxt.Text &
> "," & territorytxt.Text & "," & mech1serialnum.Text & "," &
> mech1vacpump.Text & "," & mech1spindle.Text & "," &
> mech1vacpumpserialnum.Text & "," & mech1filterpartnum.Text & "," &
> mech2serialnum.Text & "," & mech2vacpump.Text & "," & mech2spindle.Text
> & "," & mech2vacpumpserialnum.Text & "," & mech2filterpartnum.Text &
> "," & mech3serialnum.Text & "," & mech3vacpump.Text & "," &
> mech3spindle.Text & "," & mech3vacpumpserialnum.Text & "," &
> mech3filterpartnum.Text & "," & mech4serialnum.Text & "," &
> mech4vacpump.Text & "," & mech4spindle.Text & "," &
> mech4vacpumpserialnum.Text & "," & mech4filterpartnum.Text & "," &
> user1txt.Text & "," & user2txt.Text & "," & user3txt.Text & "," &
> user4txt.Text & "," & user5txt.Text & "," & user6txt.Text & "," &
> user7txt.Text & "," & user8txt.Text & "," & user9txt.Text & "," &
> user10txt.Text & "," & user11txt.Text & "," & user12txt.Text & "," &
> user13txt.Text & "," & user14txt.Text & "," & user15txt.Text
>        filename2 = "WRITE FILENAME"
>        Dim filename As String
>        filename = "READ FILENAME"
>        ' Create an empty string array to return to caller.
>        Dim lines() As String = {}
>
>        ' Check to see if the file exists.
>        If File.Exists(filename) Then
>            ' Open a stream reader to get the text from the file.
>            Dim sr As New StreamReader(filename)
>            ' Read all the file text.
>            Dim fileText As String = sr.ReadToEnd()
>            ' Close the stream reader
>            sr.Close()
>
>            ' Split the text into a string array delimited by Carriage
> Return/Line Feed.
>            lines = Split(fileText, vbCrLf)
>
>            ' Return
>            Dim fs As New FileStream(filename2, FileMode.Create)
>            Dim sw As New StreamWriter(fs)
>
>            Dim foundBlank As Boolean
>            For Each line As String In lines
>                If line.Length > 0 Then
>                    line_num += 1
>                    If line_num = currecord Then
>                        sw.WriteLine(csv_text)
>                        ' Reset blank line flag
>                        foundBlank = False
>                    Else
>                        sw.WriteLine(line)
>                        ' Reset blank line flag
>                        foundBlank = False
>                    End If
>                Else
>                    If Not foundBlank Then
>                        ' Blank line: write first one
>                        sw.WriteLine()
>                        ' Set flag to indicate that blank line was
> found
>                        foundBlank = True
>                    End If
>                End If
>            Next
>            sw.Close()
>            fs.Close()
>        End If
>        If System.IO.File.Exists(filename2) = True Then
>            If System.IO.File.Exists(filename) = True Then
>                System.IO.File.Delete(filename)
>            End If
>            System.IO.File.Copy(filename2, filename)
>        End If
>    End Sub
>
>
>
> -- END CODE --
> --CSV format--
> client_id,company,Name,addr,city,state,zip,country,phone,fax,cell,title,assistant,url,altcontact,altphone,altaddr,altcity,altstate,altzip,territory,mech1serialnum,mech1vacpump,mech1spindle,mech1vacpumpserialnum,mech1filterpartnum,mech2serialnum,mech2vacpump,mech2spindle,mech2vacpumpserialnum,mech2filterpartnum,mech3serialnum,mech3vacpump,mech3spindle,mech3vacpumpserialnum,mech3filterpartnum,mech4serialnum,mech4vacpump,mech4spindle,mech4vacpumpserialnum,mech4filterpartnum,user1,user2,user3,user4,user5,user6,user7,user8,user9,user10,user11,user12,user13,user14,user15
> --end csv format--
>