How to retrieve data from a data file that is stored on the computer and display that data in a textbox in VB?
-
I am trying to display data in a text, however the data is stored in a data file that is on the computer. The program I am trying to create is a payroll system, where by the employee first saves his details using the form, and the employees details are stored on file named data.txt. I have the code here, but for some reason it does seem to work. Can someone please help me... Public Class frmPayroll Public HourlyRate As Integer = 7.5 Const FileName = "data.txt" Public FirstName, SurName As String Public HoursWorked, HoursWorked2, EmployeeNumber As Integer Public DateOfBirth As Date Public NationalInsuranceNumber As String Public OvertimeHours, TaxDeduct, NetWage, NationalInsuranceDeduct, GrossPay As Integer Public OvertimeRate As Double Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click FirstName = txtFirstName.Text SurName = txtSurname.Text HoursWorked2 = Val(txtHoursWorked2.Text) 'open the file and assume the file exists AND adds more data to the file' FileOpen(10, FileName, OpenMode.Append) WriteLine(10, Val(txtEmployeeNumber.Text)) WriteLine(10, FirstName) WriteLine(10, SurName) WriteLine(10, Val(txtHoursWorked.Text)) WriteLine(10, txtNationalInsuranceNumber.Text) WriteLine(10, dtpDateOfBirth.Value) 'Close the file' FileClose(10) FirstName = txtFirstName.Text SurName = txtSurname.Text DateOfBirth = dtpDateOfBirth.Value lstDataFile.Items.Clear() End Sub Private Sub lstDataFile_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDataFile.SelectedIndexChanged Dim temp1 As Integer Dim temp2, temp3 As String Try FileOpen(11, FileName, OpenMode.Input) While Not EOF(11) 'get data' temp1 = LineInput(11) temp2 = LineInput(11) temp3 = LineInput(11) If (temp2 + " " + temp3) = lstDataFile.SelectedItem Then EmployeeNumber = temp1 FirstName = temp2 SurName = temp3 HoursWorked = LineInput(11) NationalInsuranceNumber = LineInput(11) DateOfBirth = LineInput(11) End If End While FileClose(11) Catch ex As Exception MsgBox("Check Employee Exists") End Try If HoursWorked > 40 Then OvertimeHours = HoursWorked2 - 40 HoursWorked2 = txtHoursWorked2.Text txtHoursWorked2.Text = HoursWorked2 txtHoursWorked2.Text = HoursWorked2 txtOvertimeHours.Text = OvertimeHours txtOvertimeRate.Text = "£7.50" Else txtOvertimeHours.Text = "0" txtOvertimeRate.Text = "£0" End If NetWage = HourlyRate * HoursWorked GrossPay = NetWage + (OvertimeHours * HourlyRate) txtGrossPay.Text = GrossPay If DateOfBirth > "31 / 12 / 1995" Then TaxDeduct = NetWage / 100 * 18 txtTaxDeducted.Text = TaxDeduct ElseIf DateOfBirth < "31 / 12 / 1994" Then TaxDeduct = NetWage / 100 * 25 txtTaxDeducted.Text = TaxDeduct ElseIf DateOfBirth < "31 / 12 / 1951" Then TaxDeduct = NetWage / 100 * 20 txtTaxDeducted.Text = TaxDeduct End If NationalInsuranceDeduct = NetWage / 100 * 7 NetWage = NationalInsuranceDeduct + TaxDeduct txtNetwage.Text = "£" & NetWage End Sub Private Sub frmPayroll_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try FileOpen(11, FileName, OpenMode.Input) While Not EOF(11) 'get data' EmployeeNumber = Val(LineInput(11)) FirstName = LineInput(11) SurName = LineInput(11) HoursWorked = Val(LineInput(11)) NationalInsuranceNumber = LineInput(11) DateOfBirth = LineInput(11) lstDataFile.Items.Add(FirstName & " " & SurName) End While FileClose(11) Catch ex As Exception MsgBox("Check Employee Exists") End Try End Sub End Class I also have a listbox, that displays the employees firstname and surname, when the user selects the employee name I want VB to calculate his grosspay, netwage etc.... PLEASE HELP, MANY THANKS....
-
Answer:
This is a VERY bad way of doing it. 1) Using a plain text file gives you some pretty week security.. and payroll information NEEDS to be secure. 2) You should be doing this with a database. Not only will it be more secure, but it will also make your programming MUCH easier. If you're going to do this with text files, you should use multiple files to mimic a database like setup, and then load those files into a dataset/datatable within your application.
hassan at Yahoo! Answers Visit the source
Other answers
Do you have an issue with you program not finding your data.txt file? I notice that you are only defining the file name and have not included a drive and path. Such as "C:\Temp\data.txt" this would be problematic especially if you have written data to a similar named file in another directory and reading a file from a different directory.
MarkG
Related Q & A:
- How to retrieve data from table using its key?Best solution by msdn.microsoft.com
- How to retrieve data from parse?Best solution by Stack Overflow
- How to Display Big Data On A Google Map?Best solution by gis.stackexchange.com
- How to scrape data from a website?Best solution by Stack Overflow
- In Visual Studio 2012 or 2013, how can I register a DLL file on Windows 7 64-bit computer using a Custom Action command?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.