You are viewing our Forum Archives. To view or take place in current topics click here.
File unable to load
Posted:

File unable to loadPosted:

Rookie_MoDDeR
  • Ladder Climber
Status: Offline
Joined: Jan 18, 201113Year Member
Posts: 328
Reputation Power: 16
Status: Offline
Joined: Jan 18, 201113Year Member
Posts: 328
Reputation Power: 16
So I'm developing a basic windows form HR Application.

I'm currently working on the load method, to load the text file collection into the program.
However when i run it i receive "Unable to load file"

Any help is much appreciated. Thank you

public bool Load(string filename)
        {

            bool status = false;
            StreamReader inputFile = null;
            string inputLine;
            filename = "employees.txt";
           

            if (!File.Exists(filename))
            {
                return false;
            }
            try
            {
                inputFile = new StreamReader(filename);
                if (inputFile != null)
                {
                    inputLine = inputFile.ReadLine();
                    while (inputLine != null)
                    {
                        Employee employee = null;
                        switch (inputLine[0])
                        {
                            case 'H':
                                employee = new HourlyEmployee(inputLine);
                                break;

                            case 'S':
                                employee = new SalariedEmployee(inputLine);
                                break;

                        }
                        // Ignore any bad records
                        if (employee != null)
                        {
                            this.Add(employee);
                        }
                        inputLine = inputFile.ReadLine();
                    }
                }
                status = true;
            }
            catch
            {
               
            }
            finally
            {
                if (inputFile != null)
                {
                    inputFile.Close();
                }
            }
            return status;
        }


 private void MainForm_Load(object sender, EventArgs e)
        {
            employees = new Employees();
            if (!employees.Load(employeesFile))
            {
                MessageBox.Show("Unable to load employees file");
            }
            else
            {
                PopulateListBox();
            }
        }


using System;
using System.Collections.Generic;
using System.Text;

namespace HRApplication
{
    public class HourlyEmployee : Employee
    {

        public decimal HourlyPay { get; set; }
        public decimal OverTimePay { get; set; }

        public HourlyEmployee()
        {           
        }

       
       
        public HourlyEmployee(string firstName,
                              string lastName,
                              string address,
                              string postCode,
                              string phoneNumber,
                              DateTime dateOfBirth,
                              decimal hourlyPay,
                              decimal overtimePay) : base (firstName, lastName, address, postCode, phoneNumber, dateOfBirth)
        {

            HourlyPay = hourlyPay;
            OverTimePay = overtimePay;
            FirstName = firstName;
            LastName = lastName;
            Address = address;
            PostCode = postCode;
            PhoneNumber = phoneNumber;
            DateOfBirth = dateOfBirth;


           

        }

        public HourlyEmployee(string persistentData) : base (persistentData)
        {
            string[] readValues = persistentData.Split(new char[] { '|' });

           
            HourlyPay = (int.Parse(readValues[9]));
            OverTimePay = (int.Parse(readValues[10]));

           
        }


    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace HRApplication
{
    public class Employee
    {
       
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string PostCode { get; set; }
        public string PhoneNumber { get; set; }     
        public DateTime DateOfBirth { get; set; }


        public Employee()
        {
        }
        public Employee(string firstName,
                        string lastName,
                        string address,
                        string postCode,
                        string phoneNumber,
                        DateTime dateOfBirth)
        {
            FirstName = firstName;
            LastName = lastName;
            Address = address;
            PostCode = postCode;
            PhoneNumber = phoneNumber;
            DateOfBirth = dateOfBirth;
        }
        public Employee(string persistentData)
        {
            string[] readValues = persistentData.Split(new char[] { '|' });


            FirstName = readValues[1];
            LastName = readValues[2];
            Address = readValues[3];
            PostCode = readValues[4];
            PhoneNumber = readValues[5];
            DateOfBirth = DateTime.Parse(readValues[6]);
                          DateTime.Parse(readValues[7]);
                          DateTime.Parse(readValues[8]);



        }



    }

   
}


using System;
using System.Collections.Generic;
using System.Text;

namespace HRApplication
{
    public class SalariedEmployee : Employee
    {

        public decimal Salary { get; set; }

        public SalariedEmployee()
        {

           
        }
       
       
        public SalariedEmployee(string firstName,
                                string lastName,
                                string address,
                                string postCode,
                                string phoneNumber,
                                DateTime dateOfBirth,
                                decimal salary) : base (firstName, lastName, address, postCode, phoneNumber, dateOfBirth)
        {
            Salary = salary;
            FirstName = firstName;
            LastName = lastName;
            Address = address;
            PostCode = postCode;
            PhoneNumber = phoneNumber;
            DateOfBirth = dateOfBirth;
        }

        public SalariedEmployee(string persistentData)
        {
            string[] readValues = persistentData.Split(new char[] { '|' });

           
            Salary = int.Parse(readValues[9]);
           
        }


    }
}
#2. Posted:
Cyimking
  • TTG Senior
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
In the Load(String filename) function, catch the exception to see what the actual error is. It could be a simple directory issue.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.