Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So i have made a visual basic program that records employee data and saves it to

ID: 3812489 • Letter: S

Question

So i have made a visual basic program that records employee data and saves it to a txt file. This is my code:

'This program will demonstrate how to save the employee details into file

'Import the package

Imports System.IO

Public Class Form1

    ' Declare global variables

    Dim fileName As String

    Dim firstName As String

    Dim middleName As String

    Dim lastName As String

    Dim employeeNumber As Integer

    Dim department As String

    Dim telephone As String

    Dim extension As Integer

    Dim emailAddress As String

    Dim valid As Boolean = True

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

        Do

            fileName = InputBox("Input Needed", "Enter the name of the file.")

            If fileName = Nothing Or fileName = "" Then

                MessageBox.Show("No file name entered.")

            Else

                Exit Do

            End If

        Loop

    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

        valid = True ' reset value to account for prior invalid input

        InputData()

        If valid = True Then

            WriteDataToFile()

        Else

            InputData()

        End If

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

        'Clear the all text field details

        txtFirstName.Text = ""

        txtMiddleName.Text = ""

        txtLastName.Text = ""

        txtEmpNumber.Text = ""

        cboDepartment.SelectedIndex = -1

        txtTelephone.Text = ""

        txtExtension.Text = ""

        txtEmail.Text = ""

    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click

        'Close the form

        Me.Close()

    End Sub

    Sub InputData()

        ' populate the variables

        firstName = txtFirstName.Text

        If (firstName.ToUpper >= "A") And (firstName.ToUpper <= "Z") Then

            firstName = firstName

        Else

            valid = False

            MessageBox.Show("First Name must start with a letter")

        End If

        middleName = txtMiddleName.Text

        If (middleName.ToUpper >= "A") And (middleName.ToUpper <= "Z") Then

            middleName = middleName

        Else

            valid = False

            MessageBox.Show("Middle Name must start with a letter")

        End If

        lastName = txtLastName.Text

        If (lastName.ToUpper >= "A") And (lastName.ToUpper <= "Z") Then

            lastName = lastName

        Else

            valid = False

            MessageBox.Show("Last Name must start with a letter")

        End If

        Try

            employeeNumber = CInt(txtEmpNumber.Text)

        Catch

            MessageBox.Show("You must enter an integer for Employee Number.")

            valid = False

        End Try

        If cboDepartment.SelectedIndex <> -1 Then

            department = cboDepartment.Text

        Else

            valid = False

            MessageBox.Show("You must select a department.")

        End If

        telephone = txtTelephone.Text

        Try

            extension = CInt(txtExtension.Text)

        Catch

            MessageBox.Show("You must enter an integer for Extension.")

            valid = False

        End Try

        emailAddress = txtEmail.Text

    End Sub

    Sub WriteDataToFile()

        ' write the data to the file

        Dim sw As StreamWriter = File.AppendText(fileName)

        sw.WriteLine(firstName)

        sw.WriteLine(middleName)

        sw.WriteLine(lastName)

        sw.WriteLine(employeeNumber)

        sw.WriteLine(department)

        sw.WriteLine(telephone)

        sw.WriteLine(extension)

        sw.WriteLine(emailAddress)

        sw.Close()

        MessageBox.Show("Record Saved. Please clear the form and enter additional records or exit.")

    End Sub

End Class

Now i have been Tasked with making a program the retieves the data that was stored. Something appears to be wrong with my code. Please Help!!

This is my code for the second part: I have marked all of the code errors i am getting in bold

'This program demonstrates how to save the employee details into a file
'Import the file
Imports System.IO
Public Class Form1
'Global variable declarations
Dim txtFile As StreamWriter
Dim fileName As String
'To keep track record number
Dim recordno As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Get the file name from the user
fileName = InputBox("Enter the File Name:", "File Name")
'Open a text file already created
txtFile = File.OpenText(fileName)
'Call setData()Function to set the data
setData()
End Sub

Public Sub setData()
'Increment the record number by 1
recordno += 1
'Check whether the file is not end of file
If Not txtFile.EndOfStream Then
'set the record number to appropriate text field
txtRecordNumber.Text = recordno
'set the first name to appropriate text field
txtFirstName.Text = txtFile.ReadLine()
'Set the middle name to appropriate text field
txtMiddleName.Text = txtFile.ReadLine()
'set the last name to appropriate text field
txtLastName.Text = txtFileRealLine()
'Set the employee number to appropriate text field
txtEmpNumber.Text = txtFile.ReadLine()
'set the department to appropriate text field
txtDept.Text = txtFile.ReadLine()
'set the phone number to appropriate text field
txtTelephone.Text = txtFile.ReadLine()
'set the extension to appropriate text field
txtExtension.Text = txtFile.ReadLine()
'set the email id to appropriate text field
txtEmail.Text = txtFile.ReadLine()
Else
'if end of file reached message box will be displayed
MessageBox.Show("End of file reached")
End If
End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'Clear the all text field details
txtFirstName.Text = ""
txtMiddleName.Text = ""
txtLastName.Text = ""
txtEmpNumber.Text = ""
txtDept.Text = ""
txtTelephone.Text = ""
txtExtension.Text = ""
txtEmail.Text = ""

End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
'Call setData() Function
setData()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Close the form
Me.Close()
End Sub
End Class

Explanation / Answer

1. The errors are occuring in your program because you have not used StreamReader class to read the file. Similarly as you have used StreamWriter class in your first program to write data to text file.

2. If your first program works properly then your text file will contain following information in the given order blow(example):

firstName

         middleName

         lastName

         employeeNumber

         department

         telephone

         extension

         emailAddress

Make sure that the file is readable, open it in a text reader such as “notepad” in windows OS and verify that the data entered by your program is in readable format. If the file is not in readable format then add file-extension along with the file name such as “anyFileName.txt”. Modify your first program accordingly to input file name with file-extension name.

3. In your second program do the following changes:

'Get the file name from the user and make sure the file-extension is provided

'by the user for example: anyFileName.txt

        fileName = InputBox("Enter the File Name:", "File Name")

And Replace this line in your code

'Open a text file already created

        txtFile = File.OpenText(fileName)

with the code provided below

' Create an instance of StreamReader to read data from the file.

' The using statement also closes the StreamReader.

' This will read the file line-by-line so in the first line it will read the firstName

' and again in second line it will read the mddleName and so on…till the end of the file.

Dim lineNumber As Integer = 0

Using sr As StreamReader = New StreamReader(fileName)

'Modify/Repace Public Sub setData() in your code with the code given below:

              Dim line As String

              ' Read and display lines from the file until the end of

              ' the file is reached.

              While (line <> Nothing)

                  line = sr.ReadLine()

lineNumber = lineNumber +1;

If(lineNumber == 1) Then

txtRecordNumber.Text = lineNumber

txtFirstName.Text = line

End If

If(lineNumber == 2) Then

txtMiddleName.Text = line

End If

If(lineNumber == 3) Then

txtLastName.Text = line

End If

'Fill all the text boxes similarly as above using if condition

'to avoid setting data into wrong text box

              End While

          End Using

      Catch e As Exception

          ' Let the user know what went wrong.

MessageBox.Show("The file could not be read: "+e.Message)

      End Try

4. Follow all the steps carefully and please do let me know if in case you face any errors/bugs/doubts, i will try my best to explain. Hope this helps in solving your task…Cheers !

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote