public bool InsertStaff(Staff staff)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings.Get("connectionString"));

            string subject = null, post = null;
            int    classAssigned = 0;

            switch (staff.StaffType)
            {
            case StaffType.teachingStaff:
                TeachingStaff tStaff = (TeachingStaff)staff;
                subject       = tStaff.Subject;
                classAssigned = Convert.ToInt32(tStaff.AssignedClass);
                break;

            case StaffType.administrativeStaff:
                AdminstrativeStaff aStaff = (AdminstrativeStaff)staff;
                post = aStaff.Post;
                break;

            case StaffType.supportStaff:
                SupportStaff sStaff = (SupportStaff)staff;
                post = sStaff.Post;
                break;
            }


            try
            {
                con.Open();
                SqlCommand sqlCommand = new SqlCommand("Proc_Staff_Insert", con);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.Parameters.AddWithValue("@Name", staff.Name);
                sqlCommand.Parameters.AddWithValue("@PhoneNumber", staff.PhoneNumber);
                sqlCommand.Parameters.AddWithValue("@Salary", staff.Salary);
                sqlCommand.Parameters.AddWithValue("@HouseName", staff.Address.HouseName);
                sqlCommand.Parameters.AddWithValue("@City", staff.Address.City);
                sqlCommand.Parameters.AddWithValue("@State", staff.Address.State);
                sqlCommand.Parameters.AddWithValue("@PinCode", staff.Address.Pin);
                sqlCommand.Parameters.AddWithValue("@StaffType", (int)staff.StaffType);
                sqlCommand.Parameters.AddWithValue("@Subject", subject);
                sqlCommand.Parameters.AddWithValue("@Post", post);
                sqlCommand.Parameters.AddWithValue("@ClassAssigned", classAssigned);
                sqlCommand.ExecuteNonQuery();
                sqlCommand.Dispose();
                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                con.Close();
            }
        }
        private static List <Staff> Populate(SqlDataReader sqlDataReader)
        {
            List <Staff> staffList = new List <Staff>();

            while (sqlDataReader.Read())
            {
                StaffType staffType = (StaffType)Enum.ToObject(typeof(StaffType), sqlDataReader.GetValue(12));


                switch (staffType)
                {
                case StaffType.teachingStaff:
                    TeachingStaff tStaff = new TeachingStaff();

                    PopulateCommonDetails(tStaff);

                    tStaff.Subject       = sqlDataReader.GetValue(8).ToString();
                    tStaff.AssignedClass = sqlDataReader.GetValue(9).ToString();
                    staffList.Add(tStaff);
                    break;

                case StaffType.administrativeStaff:
                    AdminstrativeStaff aStaff = new AdminstrativeStaff();
                    PopulateCommonDetails(aStaff);
                    aStaff.Post = sqlDataReader.GetValue(10).ToString();
                    staffList.Add(aStaff);

                    break;

                case StaffType.supportStaff:
                    SupportStaff sStaff = new SupportStaff();
                    PopulateCommonDetails(sStaff);
                    sStaff.Post = sqlDataReader.GetValue(11).ToString();
                    staffList.Add(sStaff);

                    break;
                }
                void PopulateCommonDetails(Staff staff)
                {
                    staff.StaffId           = Convert.ToInt32(sqlDataReader.GetValue(0));
                    staff.Name              = sqlDataReader.GetValue(1).ToString();
                    staff.PhoneNumber       = sqlDataReader.GetValue(2).ToString();
                    staff.Salary            = Convert.ToInt64(sqlDataReader.GetValue(3));
                    staff.Address.HouseName = sqlDataReader.GetValue(4).ToString();
                    staff.Address.City      = sqlDataReader.GetValue(5).ToString();
                    staff.Address.State     = sqlDataReader.GetValue(6).ToString();
                    staff.Address.Pin       = Convert.ToInt32(sqlDataReader.GetValue(7));
                }
            }
            return(staffList);
        }
Beispiel #3
0
        public bool UpdateStaff(int staffID, string name, string phoneNumber, double salary, Address address, string specificData)
        {
            Staff staffFound = FindStaff(staffID);

            if (staffFound != null)
            {
                //StaffHelper.Update(staffFound);
                staffFound.Name        = name;
                staffFound.PhoneNumber = phoneNumber;
                staffFound.Salary      = salary;
                staffFound.Address     = address;

                switch (staffFound.StaffType)
                {
                case StaffType.teachingStaff:
                    TeachingStaff tStaff = (TeachingStaff)FindStaff(staffID);
                    tStaff.AssignedClass = specificData;
                    break;

                case StaffType.administrativeStaff:
                    AdminstrativeStaff aStaff = (AdminstrativeStaff)FindStaff(staffID);
                    aStaff.Post = specificData;
                    break;

                case StaffType.supportStaff:
                    SupportStaff sStaff = (SupportStaff)FindStaff(staffID);
                    sStaff.Post = specificData;
                    break;
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }