Question

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.

Was it helpful?

Solution

You are not adding the new Employee to the list anywhere. From the code you included, you are only populating the list via populate().

I would add the new Employee to the EmployeeDB on button click. This will cause the list to be up-to-date. Then I would add a method to your EmployeeDB class that writes the file.

private void btnCreate_Click(object sender, EventArgs e)
{
    string username = txtUsername.Text;
    int i = employee.employees.Count();
    while (employee.AsEnumerable().Select(r => r.username == username).Count() > 0)
    {
        username = txtUserName.Text + i++;  //Makes sure you have no common usernames
    }
    employee.employees.Add(new Employee(){...});
    employee.SaveFile(); //New method  
}

I am assuming you have a string in the class Employee called username hence the r => r.username above

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top