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