Results 1 to 5 of 5

Thread: [RESOLVED] help with csv files and arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    8

    Resolved [RESOLVED] help with csv files and arrays

    Hi all, so im new to VB.net, What I need to do is open a csv file which contains 4 fields and many rows, then I want to do different operations determined on the value in the fields.

    for example :

    My CSV will be in the following format.

    ID,Name,Number,Status

    Heres what I have so far
    Code:
     Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    
            Dim strm As System.IO.Stream
            strm = OpenFileDialog1.OpenFile()
            TextBox1.Text = OpenFileDialog1.FileName.ToString()
            If Not (strm Is Nothing) Then
    
                Dim filestream As StreamReader
                filestream = File.OpenText(OpenFileDialog1.FileName.ToString())
                Dim readcontents As String
                Dim counter As Integer
                Dim Fails As Integer
                Dim Delivered As Integer
    
                Do Until filestream.EndOfStream
                    readcontents = filestream.ReadLine()
                    Dim textdelimiter As String
                    textdelimiter = ","
                    Dim splitout = Split(readcontents, textdelimiter)
    
                    counter = counter + 1
    
                    'Check the values of each field
    
                    
                Loop
                filestream.Close()
    
                strm.Close()
                MessageBox.Show("counter:" & counter & "Records in file")
                MessageBox.Show("Operation Complete")
            End If
    
        End Sub

    My issue is that whilst in the loop I want to extract the data from the array and assign it to a variable but I don't know how to do this without receiving an out of bound exception ( I can do this simple task in perl and php but as I am new to VB.net I haven't the slightest idea how.


    Could somebody point me in the right direction, I would appreciate it alot.

    EDIT: - I want to perform an IF statement on each field of the csv.. e.g :

    if status = 'good' then
    do something
    Else
    do something else
    Last edited by minimatrix; Mar 3rd, 2011 at 08:59 AM. Reason: Missed out some information ( Now appended )

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: help with csv files and arrays

    In place of this line:
    Code:
    'Check the values of each field
    you could write something like this:
    vb.net Code:
    1. if splitout(0) = "good" then '~~~ Checks first part. ie., at index 0 of the array
    2. 'do something
    3. else
    4. 'do something for false
    5. end if

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    8

    Re: help with csv files and arrays

    thanks for your reply!
    apologies, I forgot to mention that this is what I was already attempting previously but I kept receiving an indexoutofbounds exception.

    Please see below the code I had with this test in.

    Code:
    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    
            Dim strm As System.IO.Stream
            strm = OpenFileDialog1.OpenFile()
            TextBox1.Text = OpenFileDialog1.FileName.ToString()
            If Not (strm Is Nothing) Then
    
                Dim filestream As StreamReader
                filestream = File.OpenText(OpenFileDialog1.FileName.ToString())
                Dim readcontents As String
                Dim counter As Integer
                Dim Fails As Integer
                Dim Delivered As Integer
    
                Do Until filestream.EndOfStream
                    readcontents = filestream.ReadLine()
                    Dim textdelimiter As String
                    textdelimiter = ","
                    Dim splitout = Split(readcontents, textdelimiter)
    
                    counter = counter + 1
    
                    If splitout(3) = "DELIVERED" Then
                        Delivered = Delivered + 1
                    ElseIf splitout(3) = "FAILED" Then
                        Fails = Fails + 1
                    End If
    
                    'Check the values of each field
    
                    
                Loop
                filestream.Close()
    
                strm.Close()
                MessageBox.Show("counter:" & counter & "Records in file with " & Fails & " Failures and " & Delivered & " Delivered.")
                MessageBox.Show("Operation Complete")
            End If
    
        End Sub
    Regards

    minimatrix

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: help with csv files and arrays

    Here's how I tested it:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.  
    5.         '~~~ A sample line from file
    6.         Dim tempFileContent As String = "123,Akhilesh,1,good"
    7.  
    8.         '~~~ Splitting the string
    9.         Dim strArr() As String = tempFileContent.Split(","c)
    10.  
    11.         '~~~ Checking it
    12.         If strArr(3).Trim.ToLower = "good" Then
    13.             MessageBox.Show("Wow ! You are in good condition..")
    14.         Else
    15.             MessageBox.Show("Hey... you are in bad condition")
    16.         End If
    17.  
    18.     End Sub
    19. End Class

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    8

    Re: [RESOLVED] help with csv files and arrays

    Thanks for your help .. I realised that it was my input file that was incorrect and not the code .... my bad.
    I really appreciate your help though.

    This thread has been marked as resolved.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width