For a project i have to create a program that is able to write and read a sequential file and list the contents into textboxes. So far i am able to write to the file but the problem is i am not able to retrive/read the information from the file.

addBtn_Click is where i write the information to the file
cmdFind_Click is where i read the information to the file

Can someone please help me with this.

Thank you

Option Strict On
Option Explicit On

Public Class PayrollForm

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click

        Dim path As String = "C:\Users\KeraBaby\Desktop\Payroll\Payroll Project Form\Payroll Project Form\bin\Debug\Employees.txt"
        Dim i As Integer
        Dim aryText(4) As String

        aryText(0) = Me.empIDbox.Text
        aryText(1) = Me.fnamebox.Text
        aryText(2) = Me.lnamebox.Text
        aryText(3) = Me.salbox.Text


        Dim objWriter As New System.IO.StreamWriter(path, True)

        For i = 0 To 3

            objWriter.WriteLine(aryText(i))

        Next

        objWriter.Close()
    End Sub

    Private Sub exitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitBtn.Click
        Me.Close()
    End Sub

    Private Sub clrBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clrBtn.Click

    End Sub

    Private Sub empIDbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles empIDbox.KeyPress
        If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And e.KeyChar <> Chr(8) Then
            e.Handled = True
            addBtn.Enabled = False
            MessageBox.Show("Please enter only numeric values from 0 - 9999")
            addBtn.Enabled = True
        End If
    End Sub

    Private Sub salbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles salbox.KeyPress
        If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And e.KeyChar <> Chr(8) Then
            e.Handled = True
            addBtn.Enabled = False
            MessageBox.Show("Please enter only numeric values from 0 - 150000")
            addBtn.Enabled = True
        End If
    End Sub

    Private Sub cmdFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFind.Click
        'Finds the employee associated with the employee ID entered by the user

        Dim sr As IO.StreamReader = IO.File.OpenText("C:\Users\KeraBaby\Desktop\Payroll\Payroll Project Form\Payroll Project Form\bin\Debug\Employees.txt")

        Do While (sr.Peek() <> -1)
            Dim id As Integer = CInt(sr.ReadLine)
            Dim first As String = sr.ReadLine
            Dim last As String = sr.ReadLine
            Dim salary As Decimal = CDec(sr.ReadLine)
            If id = CInt(Me.txtUpdateID.Text) Then
                Me.txtUpdateFirstName.Text = first
                Me.txtUpdateLastName.Text = last
                Me.txtSalupdate.Text = salary.ToString("C2")

                Exit Do

            End If

        Loop
        sr.Close()


    End Sub
End Class

The easiest way to write out the text is

System.IO.File.WriteAllLines(filename,
    {empIDbox.Text,fnamebox.Text,lnamebox.Text,salbox.Text})

To read it back

Dim text() As String = System.IO.File.ReadAllLines(filename)

If text(0) = txtUpdateID.Text Then
    txtUpdateFirstName.Text = text(1)
    txtUpdateLastName.Text = text(2)
    txtSalupdate.Text = CInt(text(3)).ToString("C2")

Dim sr As IO.StreamReader = IO.File.OpenText("C:\Users\KeraBaby\Desktop\Payroll\Payroll Project Form\Payroll Project Form\bin\Debug\Employees.txt")
Dim checker As String() = sr.ReadToEnd.Split(Environment.NewLine)
Dim results = (From z In checker Let s = z.Trim() Where s IsNot "" Select z.Trim()).ToArray

   If Array.Exists(results, Function(s) s.Equals(Me.txtUpdateID.Text.Trim)) Then
    Dim n = Array.FindIndex(checker, Function(d) d.Equals(Me.txtUpdateID.Text.Trim))
            Me.txtUpdateFirstName.Text = results(n + 1).ToString()
            Me.txtUpdateLastName.Text = results(n + 2).ToString()
            Me.txtSalupdate.Text = results(n + 3).ToString()
   End If

xerohomicide thank you so much your code worked but at the same time its only reading one set of results in the file.for instance if i enter a different id it enters the same information for the very first id.

If the file format is not fixed then I suggest you change it to

ID;fname;lname;salary

Then you can read the entire file into an array. Instead of searching the array you can use Filter to locate the desired record. For example, if your file contains

10211;Michael;Phelps;37.25
90210;Gwen;Philpott;23.71
40104;Sam;Beckett;73.45

then once the file is read into the array "empdata" you can locate a particular employee by

Dim rec() As String = Filter(empdata,"90210")

If Ubound(rec) <> -1 Then
    Dim flds() As String = rec(0).Split(";")
    'flds(0) = empID
    'flds(1) = fname
    'flds(2) = lname
    'flds(3) = salary
Else
    'not found
End If

@EmpK can i see the sample file that ur running? i mean the content of the file.

@xerohomicide i added two employees to the file using the program the thing is you should be able to add any amount of employees and be able to find them by their ID

this is the content i have so far in the file:
1000
Jass
Smith
450.50

1001
Nick
Gordan
600.50

@Reverend Jim every time i set it up the way that you suggested i keep getting an error right after the file path saying Comma, ')', or a valid expression continuation

VB 2010 uses imiplicit line continuation. Try

System.IO.File.WriteAllLines(filename, _
    {empIDbox.Text,fnamebox.Text,lnamebox.Text,salbox.Text})

ok this is what placed in my code

 System.IO.File.WriteAllLines("C:UsersKeraBabyDesktopPayroll - CopyPayroll Project FormPayroll Project FormbinDebugEmployees.txt", _
  {empIDbox.Text,fnamebox.Text,lnamebox.Text,salbox.Text})

im still getting the same error Comma, ')', or a valid expression continuation and Expression expected.

ok here it is i made a minor changes..

Dim sr As IO.StreamReader = IO.File.OpenText("C:\Users\KeraBaby\Desktop\Payroll\Payroll Project Form\Payroll Project Form\bin\Debug\Employees.txt")
Dim checker As String() = sr.ReadToEnd.Split(Environment.NewLine)
Dim results = (From z In checker Let s = z.Trim() Where s IsNot "" Select z.Trim()).ToArray

If Array.Exists(results, Function(s) s.Equals(txtUpdateID.Text.Trim)) Then 
   Dim n = Array.FindIndex(results, Function(d) d.Equals(txtUpdateID.Text.Trim)) 
   Me.txtUpdateFirstName.Text = results(n + 1).ToString()
   Me.txtUpdateLastName.Text = results(n + 2).ToString()
   Me.txtSalupdate.Text = results(n + 3).ToString()
End If

What version of VB are you using? Mine is Visual Studio 2010. Perhaps yours does not support using literal arrays. If that is the case you might try

System.IO.File.WriteAllText(filename, _
    empIDbox.Text & vbCrLf & _
    fnamebox.Text & vbCrLf & _
    lnamebox.Text & vbCrLf & _
    salbox.Text   & vbCrLf)

Thank you guys so much for your help ... i got it to work perfectly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.