예제 #1
0
 private void EditBtnAdd_Click(object sender, EventArgs e)
 {
     if (CheckEmptyFields())
     {
         return;
     }
     EmployeesHandler.AddEmployee(GetEmployeeFromFields());
     this.DialogResult = DialogResult.OK;
     this.Close();
 }
        public void Save()
        {
            currEmployee = new Employee();

            //string _name = textBoxName.Text;
            //string _fatherName = textBoxFatherName.Text;
            //int _salary = Convert.ToInt32(textBoxSalary.Text);

            #region Validation for valid employee data

            //Check if all text boxes are empty

            IEnumerable <TextBox> textBoxcollection = EntryGrid.Children.OfType <TextBox>();

            foreach (TextBox box in textBoxcollection)
            {
                if (string.IsNullOrWhiteSpace(box.Text))
                {
                    MessageBox.Show("Kindly Fill all the boxes");
                    return;
                }
            }

            //CheckTextBoxes(this);


            #endregion

            try
            {
                currEmployee.Name             = textBoxName.Text;
                currEmployee.FatherName       = textBoxFatherName.Text;
                currEmployee.CNIC_No          = textBoxCNIC.Text;
                currEmployee.CurrentAddress   = textBoxCurrentAddress.Text;
                currEmployee.DutyDescription  = textBoxDescription.Text;
                currEmployee.PermanentAddress = textBoxPermanentAddress.Text;
                currEmployee.Salary           = Convert.ToInt32(textBoxSalary.Text);


                currEmployee.BirthDate   = datePickerDOB.SelectedDate.Value;
                currEmployee.JoiningDate = datePickerJoiningDate.SelectedDate.Value;

                //This line requires some care - 26 Dec
                //currEmployee.Role = new EmployeeRole(roleComboBox.SelectedIndex + 1, roleComboBox.SelectedItem.ToString());

                //Fixed: 31st Dec. Now its non-trivial way.
                currEmployee.Role = (EmployeeRole)roleComboBox.SelectedItem;


                //Insert it into Database

                empHandler = new EmployeesHandler();

                empHandler.AddEmployee(currEmployee);
            }

            catch (FormatException fexcept)
            {
                MessageBox.Show("Kindly Enter the Salary in correct (numeric) format.", "Invalid Entry");
                return;
            }

            //Added - 01 Jan
            //For Name/Father name not containing numbers

            catch (ArgumentException aexcept)
            {
                MessageBox.Show(aexcept.Message, "Invalid Entry!");
                return;
            }

            catch (InvalidOperationException invalidexcept)
            {
                MessageBox.Show(invalidexcept.Message, "Invalid Entry!");
                return;
            }

            catch (Exception except)
            {
                MessageBox.Show(except.Message, "Invalid Entry!");
                return;
            }


            //Clear all the TextBoxes

            foreach (TextBox box in textBoxcollection)
            {
                box.Text = "";
            }
        }