user3516240 user3516240 - 1 year ago 56 Question

How can I search CSV File for two Values (VB)

could someone show me how to search my CSV file for two values? My CSV File looks like this:

2,"Julius", "Short", "2564"

And I want to confirm that the 4545 number matches the name Garry in the same row. For example, a user may enter 4545 and the name Garry and the code would check if the csv matches those two values. But the name and number must match, not just a single value.

I'm not sure on how to load the csv file into my visual basic or how to search it. So any help would be greatly appreciated. I have been looking around online for the past two hours but nothing seems to be working for me so far.

Public Function CheckRegistrationKey(ByVal Name As String, ByVal Number As Integer)

Dim filepath As String = My.Computer.FileSystem.SpecialDirectories.Desktop("\File1.csv")
Dim Key As String

Dim NameToSearhFor As String = Name
Dim NumberToSearchFor As Integer = Number

'Load file

'Search file for values

'return true if found
'return false if not found

End Function


'Load file
Using MyReader As New FileIO.TextFieldParser(filepath)
MyReader.TextFieldType = FileIO.FieldType.Delimited
Dim currentRow As String()
While Not MyReader.EndOfData
currentRow = MyReader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
Catch ex As Microsoft.VisualBasic.
End Try
End While
End Using

Now I just need to work out how to search the values.

Answer Source

If you only ever need to check one user once then your proposed code will suffice, but if you need to check the users more than once then something more like this could be better:

Option Infer On
Option Strict On

Module Module1

    Dim data As List(Of datum)

    Public Class datum
        Property ID As Integer
        Property FirstName As String
        Property LastName As String
        Property Key As Integer
    End Class

    Function LoadData(srcFile As String) As List(Of datum)
        Dim data As New List(Of datum)

        Using tfp As New FileIO.TextFieldParser(srcFile)
            tfp.TextFieldType = FileIO.FieldType.Delimited
            tfp.HasFieldsEnclosedInQuotes = True

            Dim currentRow As String()
            Dim currentLine = 0

            While Not tfp.EndOfData
                currentLine += 1
                    currentRow = tfp.ReadFields()
                    If currentRow.Length = 4 Then
                        data.Add(New datum With {.ID = CInt(currentRow(0)), .FirstName = currentRow(1), .LastName = currentRow(2), .Key = CInt(currentRow(3))})
                    End If
                Catch ex As Exception
                    MsgBox($"Error in file {srcFile} at line {currentLine}: {ex.Message}")
                End Try
            End While
        End Using

        Return data

    End Function

    Function IsValidUser(firstname As String, key As Integer) As Boolean
        If data Is Nothing Then
            Throw New Exception("Data has not been initialised.")
        End If

        Return data.Any(Function(d) d.FirstName = firstname AndAlso d.Key = key)

    End Function

    Sub Main()
        Dim srcFile = "C:\temp\sampledata2.txt"
        data = LoadData(srcFile)

        For Each user In {"Garry", "Harry"}
            Console.WriteLine($"Found {user}: " & IsValidUser(user, 4545).ToString())


    End Sub

End Module
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download