コード例 #1
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (lblFName.Content == "?" || lblLName.Content == "?" || lblMI.Content == "?" || lblEmail.Content == "?" || lblSuffix.Content == "?" || lblEmail.Content == "?"
                    || String.IsNullOrWhiteSpace(txtFName.Text) || String.IsNullOrWhiteSpace(txtLName.Text) || String.IsNullOrWhiteSpace(cmbPosition.Text) || String.IsNullOrWhiteSpace(cmbDept.Text))
                {
                    System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                if (status == "Add")
                {

                    DialogResult dr = System.Windows.Forms.MessageBox.Show("Are you sure you want to add this record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == System.Windows.Forms.DialogResult.No)
                    {
                        return;
                    }

                    if (txtFName.Text == "" || txtLName.Text == "" || cmbPosition.Text == "" || cmbDept.Text == "")
                    {
                        System.Windows.MessageBox.Show("Please complete the required information", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    if (!Regex.IsMatch(txtFName.Text, @"^[a-zA-Z]+$"))
                    { 
                        
                    }
                    

                    using (var ctx = new finalContext())
                    {
                        int pID = ctx.Positions.Where(x=>x.PositionName==cmbPosition.Text).First().PositionID;
                        Employee emp = new Employee { FirstName = txtFName.Text, LastName = txtLName.Text, PositionID = pID, Suffix = txtSuffix.Text, MI = txtMI.Text, Active = true, Department = cmbDept.Text, Email = txtEmail.Text, Photo = ConvertImageToByteArray(selectedFileName) };

                        var query = from con in ctx.TempAdresses
                                    select con;
                        foreach (var item in query)
                        {
                            EmployeeAddress add = new EmployeeAddress { Street = item.Street, City = item.City, Province = item.Province };
                            ctx.EmployeeAddresses.Add(add);
                        }

                        var query1 = from con in ctx.TempContacts
                                     select con;
                        foreach (var item in query1)
                        {
                            EmployeeContact con = new EmployeeContact { Contact = item.Contact };
                            ctx.EmployeeContacts.Add(con);
                        }
                        AuditTrail at = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Added new Employee " + txtFName.Text + " " + txtMI.Text + " " + txtLName.Text + " " + txtSuffix.Text };
                        ctx.AuditTrails.Add(at);

                        ctx.Employees.Add(emp);
                        ctx.SaveChanges();
                        System.Windows.MessageBox.Show("Employee has been successfully added", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.Close();


                    }
                }
                else
                {

                    DialogResult dr = System.Windows.Forms.MessageBox.Show("Are you sure you want to update this record?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == System.Windows.Forms.DialogResult.No)
                    {
                        return;
                    }
                    using (var ctx = new finalContext())
                    {
                        int pID = ctx.Positions.Where(x => x.PositionName == cmbPosition.Text).First().PositionID;
                        var emp = ctx.Employees.Find(uId);
                        emp.FirstName = txtFName.Text;
                        emp.LastName = txtLName.Text;
                        emp.PositionID = pID;
                        emp.Suffix = txtSuffix.Text;
                        emp.MI = txtMI.Text;
                        emp.Department = cmbDept.Text;
                        emp.Email = txtEmail.Text;
                        if (isChanged == true)
                        {
                            emp.Photo = ConvertImageToByteArray(selectedFileName);
                        }
                        AuditTrail at = new AuditTrail { EmployeeID = UserID, DateAndTime = DateTime.Now, Action = "Updated Employee " + txtFName.Text + " " + txtMI.Text + " " + txtLName.Text + " " + txtSuffix.Text };
                        ctx.AuditTrails.Add(at);
                        ctx.SaveChanges();
                        System.Windows.MessageBox.Show("Employee has been successfully updated", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }
コード例 #2
0
        private void btnAddContact_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (btnAddContact.Content.ToString() != "Add")
                {
                    if (lblContact.Content == "?" || String.IsNullOrWhiteSpace(txtContact.Text))
                    {
                        System.Windows.MessageBox.Show("Please input correct format and/or fill all required fields", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }
                }

                if (btnAddContact.Content.ToString() == "Add")
                {
                    grpContact.Visibility = Visibility.Visible;
                    btnAddContact.Content = "Save";
                    btnEdtContact.Content = "Cancel";
                    btnDelContact.Visibility = Visibility.Hidden;
                    btnEdtContact.IsEnabled = true;
                    tbAddress.IsEnabled = false;
                    tbContact.IsEnabled = false;
                    tbInfo.IsEnabled = false;
                }
                else if (btnAddContact.Content.ToString() == "Save")
                {
                    if (txtContact.Text == "")
                    {
                        System.Windows.MessageBox.Show("Please complete the required information", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    //for view
                    if (status == "View")
                    {
                        using (var ctx = new finalContext())
                        {
                            EmployeeContact empCont = new EmployeeContact { Contact = txtContact.Text, EmployeeID = uId };
                            ctx.EmployeeContacts.Add(empCont);
                            ctx.SaveChanges();
                            var cont = from cn in ctx.EmployeeContacts
                                       where cn.EmployeeID == uId
                                       select new { cn.EmpContactID, cn.Contact };
                            dgContact.ItemsSource = cont.ToList();
                        }
                        reset();
                        return;
                    }

                    using (var ctx = new finalContext())
                    {
                        TempContact con = new TempContact { Contact = txtContact.Text };
                        ctx.TempContacts.Add(con);
                        ctx.SaveChanges();
                        dgContact.ItemsSource = ctx.TempContacts.ToList();
                    }

                    reset();
                }
                else
                {
                    //for view
                    if (status == "View")
                    {
                        using (var ctx = new finalContext())
                        {
                            var conts = ctx.EmployeeContacts.Find(Convert.ToInt32(getRow(dgAddress, 0)));
                            conts.Contact = txtContact.Text;
                            ctx.SaveChanges();
                            var cont = from cn in ctx.EmployeeContacts
                                       where cn.EmployeeID == uId
                                       select new { cn.EmpContactID, cn.Contact };
                            dgContact.ItemsSource = cont.ToList();
                        }
                        reset();
                        return;
                    }

                    using (var ctx = new finalContext())
                    {
                        var con = ctx.TempContacts.Find(Convert.ToInt32(getRow(dgContact, 0)));
                        con.Contact = txtContact.Text;
                        ctx.SaveChanges();
                        dgContact.ItemsSource = ctx.TempContacts.ToList();
                        reset();

                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Runtime Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }