VB.NET - Read CSV file, look for specific data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PBXHowTos
    New Member
    • Mar 2013
    • 20

    VB.NET - Read CSV file, look for specific data

    Here is the deal:

    I have a basic VB.NET (2012) app that I have coded to look for a specific CSV file. But that is where it stops cause I am stuck.

    what I need to happen is when I click a button, I want a window to pop up for me to locate the CSV file.

    THEN, I need my app to process said CSV file looking for a keyword. In this case: User_Licenses
    Once it finds it, It needs to count the TRUE's below it till it hit an empty cell and populate a label with the number. (See image below for the CSV sample)


  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please show us the code you've tried and tell us what problem you're having with it.

    Comment

    • PBXHowTos
      New Member
      • Mar 2013
      • 20

      #3
      Let me clarify, the only thing I have been able to do successfully is search for the word User_Licenses and I don't know what to do after that point. I know I need to do some kind of loop and look for a blank cell but just frankly don't know the proper/efficient way of doing it. I will continue to search the net.

      Comment

      • Mikkeee
        New Member
        • Feb 2013
        • 94

        #4
        Still need to see your code.

        Comment

        • PBXHowTos
          New Member
          • Mar 2013
          • 20

          #5
          All I am doing at this point is opening the CSV file and populating it into a DataGrid. I don't want to use the datagrid if I don't have to but what ever I can do to simply populate a label with the number of TRUE's I will be happy.
          Code:
          Imports System.IO
          
          Public Class Form1
          
              Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                  Label4.Visible = False
                  Label5.Visible = False
              End Sub
          
              Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
                  Dim SR As New StreamReader("c:\Configuration.csv")
                  For Each line As String In System.IO.File.ReadAllLines("C:\Configuration.csv")
                      DataGridView1.Rows.Add(line.Split(","))
                  Next
          
                  Label5.Text = "C:\Configuration.csv"
                  Label5.Visible = True
                  Label4.Text = "#" & " Users Licensed"
                  Label4.Visible = True
              End Sub
          End Class

          Comment

          • Mikkeee
            New Member
            • Feb 2013
            • 94

            #6
            This should get you the results you need.
            Code:
                    Dim startCounting As Boolean = False
                    Dim trueCount As Integer = 0
                    Dim csvFields() As String
                    For Each line As String In System.IO.File.ReadAllLines("C:\Configuration.csv")
                        csvFields = line.Split(",")
                        If startCounting Then
                            If csvFields(0) = String.Empty Then
                                Exit For
                            ElseIf csvFields(1) = "True" Then
                                trueCount += 1
                            End If
                        ElseIf Not startCounting AndAlso csvFields.Length > 1 AndAlso csvFields(1) = "User_Licenses" Then
                            startCounting = True
                        End If
                    Next
                    Label4.Text = String.Format("#{0} Users Licensed", trueCount)

            Comment

            • PBXHowTos
              New Member
              • Mar 2013
              • 20

              #7
              I will give that a shot. Stand by.

              Comment

              • PBXHowTos
                New Member
                • Mar 2013
                • 20

                #8
                It just responds with this:

                #{0} Users Licensed

                No count.

                Comment

                • Mikkeee
                  New Member
                  • Feb 2013
                  • 94

                  #9
                  Show me the line of code. Looks to me like you didn't enclose it in String.Format.

                  Comment

                  • PBXHowTos
                    New Member
                    • Mar 2013
                    • 20

                    #10
                    Code:
                    Label4.Text = String.Format("#{0} Users Licensed", trueCount)

                    Comment

                    • Mikkeee
                      New Member
                      • Feb 2013
                      • 94

                      #11
                      There must be something missing in your code. String.Format will not output the curly braces around the 0 '{0}' because this is a placeholder for the variables coming in. I created a similar csv file and ran it against the exact code I posted and see the correct results. Put a breakpoint on the line which assigns a value to your Label4 textbox and see what the variable trueCount is.

                      Comment

                      • PBXHowTos
                        New Member
                        • Mar 2013
                        • 20

                        #12
                        Ok. I think I see the problem. Can you try it on your side with this config? - www.calltoapps.com/Configuration.csv

                        But search for US_VoiceMailOn instead of Users_Licensed

                        (This is a different system type but I need to do the same function)

                        Comment

                        • Mikkeee
                          New Member
                          • Feb 2013
                          • 94

                          #13
                          Nope... that's your job ;-p I'm here to help you learn, not write code for you to complete your project. Just apply the same logic as above but look at the correct column number for that fieldname in the csvFields array.

                          Comment

                          • PBXHowTos
                            New Member
                            • Mar 2013
                            • 20

                            #14
                            I love your honesty... Thanks for keeping me from being lazy. :-)

                            Comment

                            • PBXHowTos
                              New Member
                              • Mar 2013
                              • 20

                              #15
                              So which line actually reads the columns then? Is it the csvFields(#) part?

                              Comment

                              Working...