1

I have this txt file with the following information:

National_Insurence_Number;Name;Surname;Hours_Worked;Price_Per_Hour so:

eg.: aa-12-34-56-a;Peter;Smith;36;12

This data has been inputed to the txt file through a VB form which works totally fine, the problem comes when, on another form. This is what I expect it to do:

  1. The user will input into a text box the employees NI Number.
  2. The program will then search through the file that NI Number and, if found;
  3. It will fill in the appropriate text boxes with its data.

(Then the program calculates tax and national insurance which i got working fine)

So basically the problem comes telling the program to search that NI number and introduce each ";" delimited field into its corresponding text box.

Thanks for all.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178

1 Answers1

0

You just need to parse the file like a csv, you can use Microsoft.VisualBasic.FileIO.TextFieldParser to do this or you can use CSVHelper - https://github.com/JoshClose/CsvHelper

I've used csv helper in the past and it works great, it allows you to create a class with the structure of the records in your data file then imports the data into a list of these for searching.

You can look here for more info on TextFieldParser if you want to go that way - Parse Delimited CSV in .NET

Dim afile As FileIO.TextFieldParser = New FileIO.TextFieldParser(FileName)
        Dim CurrentRecord As String() ' this array will hold each line of data
        afile.TextFieldType = FileIO.FieldType.Delimited
        afile.Delimiters = New String() {";"}
        afile.HasFieldsEnclosedInQuotes = True

        ' parse the actual file
        Do While Not afile.EndOfData
            Try
                CurrentRecord = afile.ReadFields
            Catch ex As FileIO.MalformedLineException
                Stop
            End Try
        Loop

I'd recommend using CsvHelper though, the documentation is pretty good and working with objects is much easier opposed to the raw string data.

Once you have found the record you can then manually set the text of each text box on your form or use a bindingsource.

Community
  • 1
  • 1
GJKH
  • 1,715
  • 13
  • 30