I'm designing a database to hold a list of employees which is reading from a text file.
I have two forms, the first one (frmManager) acts as a view to go through the list, which i have next and previous buttons which i scroll through the employees in the list. The other form(frmAdd), can add new employees to the List. My problem is, when i update the List<>, how can i update it in frmManager? when i add a new employee, theiir attributes get written to the text file but I have to rebuild the project to show the updated list.
file that adds employees:
public class EmployeeDB
{
public List<Employee> employees;
public static EmployeeDB instance;
public static EmployeeDB Instance
{
get
{
if (instance == null)
{
instance = new EmployeeDB();
instance.populate();
}
return instance;
}
}
public EmployeeDB()
{
employees = new List<Employee>();
}
public void populate()
{
string[] parts;
foreach (string line in File.ReadAllLines("StaffList.txt"))
{
parts = line.Split(',');
employees.Add(new Employee(parts[0], parts[1], parts[2], parts[3], int.Parse(parts[4]), int.Parse(parts[5])));
}
}
}
The employee class contains just a constructor to add their details.
the form to add new employees
public partial class frmAdd : Form
{
EmployeeDB employee;
int grade;
public frmAddEmployee()
{
employee = EmployeeDB.Instance;
InitializeComponent();
}
private void btnCreate_Click(object sender, EventArgs e)
{
System.IO.StreamWriter file = new System.IO.StreamWriter("StaffList.txt", true);
foreach (Employee em in employee.employees) //To avoid username clashes
{
if (em.username == txtUsername.Text)
{
file.WriteLine(txtFName.Text + "," + txtLName.Text + "," + txtUsername.Text + employee.employees.Count()
+ "," + txtPassword.Text + "," + checkedButton().ToString() + ","
+ 0.ToString(), Environment.NewLine);
}
else
{
file.WriteLine(txtFName.Text + "," + txtLName.Text + "," + txtUsername.Text
+ "," + txtPassword.Text + "," + checkedButton().ToString() + ","
+ 0.ToString(), Environment.NewLine);
}
file.Close();
MessageBox.Show("Employee successfully added");
return;
}
I have tried recalling the EmployeeDB file hoping it would repopulate to no avail. Any help will be much appreciated.