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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mikkeee
    New Member
    • Feb 2013
    • 94

    #16
    Yes, instead of csvFields(1), change the (1) to the field number that corresponds to the field you're searching for. BTW... it's a 0 based array so if you're counting the column, subtract 1 from the actual column number you see in excel.

    Comment

    • Mikkeee
      New Member
      • Feb 2013
      • 94

      #17
      And I just noticed that you have a redundant line of code. Your original post has:
      Code:
      Dim SR As New StreamReader("c:\Configuration.csv")
      but you're never using the SR object so just remove it.

      Comment

      • PBXHowTos
        New Member
        • Mar 2013
        • 20

        #18
        I removed that code. I forgot about it.

        Comment

        • PBXHowTos
          New Member
          • Mar 2013
          • 20

          #19
          Thank you, I will try that now.

          Comment

          • PBXHowTos
            New Member
            • Mar 2013
            • 20

            #20
            I get an out of range error when I tried that.

            Here is what I updated it to.
            Code:
            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(23) = "TRUE" Then
                                trueCount += 1
                            End If
                        ElseIf Not startCounting AndAlso csvFields.Length > 1 AndAlso csvFields(23) = "US_VoiceMailOn" Then
                            startCounting = True
                        End If

            Comment

            • Mikkeee
              New Member
              • Feb 2013
              • 94

              #21
              I looked at your file an you're getting the error because you're working with a screwed up CSV file. Whoever the developer is who wrote the code to output this should receive a very hard slap on their hand. Take a look at http://en.wikipedia.org/wiki/Comma-s...s_and_examples for best practices with CSV files. I work with banks and their back end systems and see this a lot. You basically have a CSV file with combined table outputs which makes it difficult to read and requires some fairly tricky code to handle it elegantly. So enough now with my rant. All you need to do is change the line:
              Code:
              ElseIf Not startCounting AndAlso csvFields.Length => 23 AndAlso csvFields(23) = "US_VoiceMailOn" Then

              Comment

              • PBXHowTos
                New Member
                • Mar 2013
                • 20

                #22
                AND this isn't the only thing they screw up. :-)

                Thanks, I will try that.

                Comment

                • PBXHowTos
                  New Member
                  • Mar 2013
                  • 20

                  #23
                  Ok, I tried that and got the same error.

                  Code:
                  Code:
                          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(23) = "TRUE" Then
                                      trueCount += 1
                                  End If
                              ElseIf Not startCounting AndAlso csvFields.Length >= 23 AndAlso csvFields(23) = "US_VoiceMailOn" Then
                                  startCounting = True
                              End If
                          Next
                          Label4.Text = String.Format("{0} Users Licensed", trueCount)

                  Comment

                  • Mikkeee
                    New Member
                    • Feb 2013
                    • 94

                    #24
                    Looks like the record which follows the section your looking for only has a single field. Change the following:
                    Code:
                    If csvFields(0) = String.Empty Then
                    to
                    If csvFields(0) = String.Empty OrElse csvFields.Length < 23 Then

                    Comment

                    • PBXHowTos
                      New Member
                      • Mar 2013
                      • 20

                      #25
                      That seemed to fix the issue with the error. But now, it only brings back 0.

                      Comment

                      • Mikkeee
                        New Member
                        • Feb 2013
                        • 94

                        #26
                        I ran the following code against your CSV file and receive the following result:
                        #35 True Values
                        Code:
                            Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
                                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 OrElse csvFields.Length < 23 Then
                                            Exit For
                                        ElseIf csvFields(23) = "True" Then
                                            trueCount += 1
                                        End If
                                    ElseIf Not startCounting AndAlso csvFields.Length >= 23 AndAlso csvFields(23) = "US_VoiceMailOn" Then
                                        startCounting = True
                                    End If
                                Next
                                MessageBox.Show(String.Format("#{0} True Values", trueCount))
                            End Sub

                        Comment

                        • PBXHowTos
                          New Member
                          • Mar 2013
                          • 20

                          #27
                          I am not sure what I was doing but I copied that code into my app and it works. Thanks man. I appreciate your teaching. I really do.

                          Comment

                          • Mikkeee
                            New Member
                            • Feb 2013
                            • 94

                            #28
                            No problem! I've been in your situation and received a lot of help so and I've come to the point where it's time for me to pay it back.

                            Comment

                            Working...