MaMi
MaMi

Reputation: 13

VB.Net , Finding data in a text file by ID number

I'm using the VB.net forms application for this project.

so I have a text file like this

7,John,Kimberlake,[email protected],27,Bachelor

8,Tiny,Down,[email protected],34,Master

9,Jeniffer,Kime,[email protected],22,None

I have 1 textbox and 1 button. The purpose is that you need to fill an id number to find the data about the person.

Dim Findstring = IO.File.ReadAllText("data.txt")

    Dim Data As String = TextBox1.Text
    Dim aryTextFile() As String

    aryTextFile = Findstring.Split(",")
    If aryTextFile.Contains(Data) Then


    End If

I tried this and something like finding the index number in the array of the requested id but it didn't work.

Upvotes: 1

Views: 593

Answers (1)

Pavan Chandaka
Pavan Chandaka

Reputation: 12831

Instead of ReadAllText use ReadLines and loop each line to get the data.

Walk through below code and comments. There are much better ways to do this, But the below is very basic way of doing for easy understanding.

       'READ EACH LINE OF THE TEXT FILE.
        For Each item As String In IO.File.ReadLines("C:\\Desktop\\test.txt") 'ENSURE VALID PATH HERE

            'THIS IS EACH LINE.
            Dim Findstring = item

            'ASSUME THIS IS TEXT ID FROM TEXT BOX.
            Dim ID As String = "8"

            'SPLIT THE LINE BASED ON ","
            Dim aryTextLine() As String
            aryTextLine = Findstring.Split(",")

            'NOW YOU HAVE ARRAY TO READ EACH ITEM.
            If aryTextLine(0) = ID Then
                Dim name = aryTextLine(1)
            End If
        Next

Upvotes: 1

Related Questions