Exemple #1
0
        private void EmpAdd_Click(object sender, RoutedEventArgs e)
        {
            dataAccess data = new dataAccess();

            // MessageBox.Show("Continue to add a new employee ?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning);

            if (EmployeeID.Text == "" || EmployeeName.Text == "" || Email.Text == "" ||
                Address.Text == "" || ReportTo.Text == "" || Username.Text == "" || PositionID.Text == "" ||
                Password.Password.ToString() == "")
            {
                MessageBox.Show("All Fields are required to be filled");
            }
            else
            {
                if (MessageBox.Show("Continue to add a new employee ??", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
                {
                    //do no stuff
                }
                else
                {
                    //do Yes stuff
                    if (EmployeeNumber.IsEnabled == false && EmployeeNumber.IsReadOnly == true)
                    {
                        MessageBox.Show("Employee Number Field is Enabled now Enter the Employee Number of a new Employee");
                        EmployeeNumber.IsEnabled  = true;
                        EmployeeNumber.IsReadOnly = false;

                        EmployeeName.IsEnabled  = false;
                        EmployeeName.IsReadOnly = true;
                        EmployeeID.IsEnabled    = false;
                        EmployeeID.IsReadOnly   = true;
                        Address.IsEnabled       = false;
                        Address.IsReadOnly      = true;
                        Email.IsEnabled         = false;
                        Email.IsReadOnly        = true;
                        PositionID.IsEnabled    = false;
                        PositionID.IsReadOnly   = true;
                        Username.IsEnabled      = false;
                        Username.IsReadOnly     = true;
                        Password.IsEnabled      = false;
                        ReportTo.IsEnabled      = false;
                        ReportTo.IsReadOnly     = true;

                        empSearch.IsEnabled = false;
                        empDelete.IsEnabled = false;
                        empEdit.IsEnabled   = false;
                    }
                    else if (EmployeeNumber.IsEnabled == true && EmployeeNumber.IsReadOnly == false)
                    {
                        if (Email.Text.Length == 0)
                        {
                            MessageBox.Show("Enter an email.");
                            Email.Focus();
                        }
                        else if (!Regex.IsMatch(Email.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
                        {
                            MessageBox.Show("Enter a valid email.");
                            Email.Select(0, Email.Text.Length);
                            Email.Focus();
                            Email.IsEnabled  = true;
                            Email.IsReadOnly = false;
                        }
                        else
                        {
                            string status = data.insertEmployee(int.Parse(EmployeeNumber.Text), long.Parse(EmployeeID.Text), EmployeeName.Text,
                                                                Address.Text, Email.Text, Username.Text, Password.Password.ToString(), ReportTo.Text, int.Parse(PositionID.Text));

                            if (status.Equals("Successful"))
                            {
                                MessageBox.Show("New Employee Inserted");
                                EmployeeNumber.IsEnabled  = false;
                                EmployeeNumber.IsReadOnly = true;

                                EmployeeName.IsEnabled  = true;
                                EmployeeName.IsReadOnly = false;
                                EmployeeID.IsEnabled    = true;
                                EmployeeID.IsReadOnly   = false;
                                Address.IsEnabled       = true;
                                Address.IsReadOnly      = false;
                                Email.IsEnabled         = true;
                                Email.IsReadOnly        = false;
                                PositionID.IsEnabled    = true;
                                PositionID.IsReadOnly   = false;
                                Username.IsEnabled      = true;
                                Username.IsReadOnly     = false;
                                Password.IsEnabled      = true;
                                ReportTo.IsEnabled      = true;
                                ReportTo.IsReadOnly     = false;

                                empSearch.IsEnabled = true;
                                empDelete.IsEnabled = true;
                                empEdit.IsEnabled   = true;


                                EmployeeNumber.Text = "";
                                EmployeeName.Text   = "";
                                EmployeeID.Text     = "";
                                Address.Text        = "";
                                Email.Text          = "";
                                PositionID.Text     = "";
                                Username.Text       = "";
                                Password.Password   = "";
                                ReportTo.Text       = "";
                            }
                            else if (status.Equals("Unsuccessful"))
                            {
                                MessageBox.Show("Employee Number/Username Already Exist !! \n Try to use other values in these fields");
                                Username.IsEnabled  = true;
                                Username.IsReadOnly = false;
                            }
                            else
                            {
                                MessageBox.Show("Exception Error");
                            }
                        }
                    }
                }
            }
        }