private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (txtFirstName.Text.Trim() == "")
            {
                MessageBox.Show("First name was required", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (txtLastName.Text.Trim() == "")
            {
                MessageBox.Show("Last name was required", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (dtpHireDate.SelectedDate == null)
            {
                MessageBox.Show("Hire date was required", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (dtpHireDate.SelectedDate.Value.Date > DateTime.Now.Date)
            {
                MessageBox.Show("Hire date must be <= Now", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (txtPhone.Text.Trim() == "")
            {
                MessageBox.Show("Phone was required", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (Regex.IsMatch(txtPhone.Text, @"\D"))
            {
                MessageBox.Show("Phone must be digits", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            CrewMember crewMember = new CrewMember()
            {
                FirstName = txtFirstName.Text.Trim(),
                LastName  = txtLastName.Text.Trim(),
                Office    = cbCountry.SelectedItem as Office,
                Country   = cbCountry.SelectedItem as Country,
                Gender    = cbGender.Text,
                Position  = cbPosition.SelectedItem as Position,
                Phone     = txtPhone.Text,
                HireDate  = dtpHireDate.SelectedDate.Value.Date
            };

            Db.Context.CrewMembers.Add(crewMember);
            Db.Context.SaveChanges();
            ManageWindow.LoadMembers();
            MessageBox.Show("Add member successful", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            this.Close();
        }
Example #2
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (txtFirstName.Text.Trim() == "")
            {
                MessageBox.Show("First name was required", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (txtLastName.Text.Trim() == "")
            {
                MessageBox.Show("Last name was required", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (dtpHireDate.SelectedDate == null)
            {
                MessageBox.Show("Hire date was required", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (dtpHireDate.SelectedDate.Value.Date > DateTime.Now.Date)
            {
                MessageBox.Show("Hire date must be <= Now", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (txtPhone.Text.Trim() == "")
            {
                MessageBox.Show("Phone was required", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (Regex.IsMatch(txtPhone.Text, @"\D"))
            {
                MessageBox.Show("Phone must be digits", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (txtPhone.Text != Member.Phone)
            {
                if (Db.Context.CrewMembers.FirstOrDefault(t => t.Phone == txtPhone.Text) != null)
                {
                    MessageBox.Show("This phone was used", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            Member.FirstName = txtFirstName.Text.Trim();
            Member.LastName  = txtLastName.Text.Trim();
            Member.Office    = cbCountry.SelectedItem as Office;
            Member.Country   = cbCountry.SelectedItem as Country;
            Member.Gender    = cbGender.Text;
            Member.Position  = cbPosition.SelectedItem as Position;
            Member.Phone     = txtPhone.Text;
            Member.HireDate  = dtpHireDate.SelectedDate.Value.Date;

            Db.Context.SaveChanges();
            ManageWindow.LoadMembers();
            MessageBox.Show("Edit member successful", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            this.Close();
        }