Seems to me that i am weak in loops and arrays. School project need to advise that. My assignment is to write and read to a text file. I can write to the text file using streamwriter and streamreader and to append text I used the "Employee.txt", true in order to append to future data. I also used the openFileDialog to retrieve and display data to the textboxes. It will not display data but render a NullReference Exception pointing to the .Peek();. I used this method to display in another program it worked great but will not work in this program.
I have an array to limit 10 entries to the text file if exceeds and exception will occur that only 10 records are allowed then close the dialog.
I also suppose to calculate the hoursworked >40 * 1.5 * PayRate now I can't think of a way to do this becaause it has to pass through the array in a loop to get the pay and text to be displayed.
Can you help.
This is my intermediate class and can't understand this concept.
I also attached the file with the form so you could see the program.
I appreciate for your assistance.
******************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace DesiBravo_IT274_Unit6
{
public partial class Form1 : Form
{
private StreamReader employeeStreamReader;
string[] employeeData = new string[10];
int indexInteger;
public Form1()
{
InitializeComponent();
}
StreamWriter employeeStreamWriter = new StreamWriter("Employee.txt", true);
private void btnSave_Click(object sender, EventArgs e)
{
employeeStreamWriter.WriteLine(txtFirstName.Text);
employeeStreamWriter.WriteLine(txtLastName.Text);
employeeStreamWriter.WriteLine(txtPayRate.Text);
try
{
employeeData[indexInteger++] = txtFirstName.Text;
employeeData[indexInteger++] = txtLastName.Text;
employeeData[indexInteger++] = txtPayRate.Text + "\n ";
}
catch (Exception)
{
MessageBox.Show("You can only save 10 records...", "Data Error");
}
txtFirstName.Clear();
txtFirstName.Focus();
txtLastName.Clear();
txtPayRate.Clear();
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
employeeStreamWriter.Close();
this.Close();
}
private void btnNext_Click(object sender, EventArgs e)
{
Display();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult responseDialogResult;
openFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();
responseDialogResult = openFileDialog1.ShowDialog();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
employeeStreamReader = new StreamReader("Employee.txt");
Display();
}
catch
{
MessageBox.Show("File does not exist.");
}
}
private void Display()
{
if (employeeStreamReader.Peek() != -1)
{
txtFirstName.Text = employeeStreamReader.ReadLine();
txtLastName.Text = employeeStreamReader.ReadLine();
txtPayRate.Text = employeeStreamReader.ReadLine();
}
}
}
}
}