Exemplo n.º 1
0
 private void btnSave_Click(object sender, EventArgs e)
 {
     if (lastNameTextBox.Text != "" && firstNameTextBox.Text != "")
     {
         if (add == true)
         {
             db.Employees.Add(employeeBindingSource.Current as Employee);
         }
         db.SaveChanges();
         MessageBox.Show("Save!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
         Close();
     }
     else
     {
         MessageBox.Show("Please enter your Last Name and First Name.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }
Exemplo n.º 2
0
        private void btnDistribution_Click(object sender, EventArgs e)
        {
            employeeIDComboBox.DataBindings.Add("Text",
                                                employeeBindingSource,
                                                "EmployeeID");
            territoryIDComboBox.DataBindings.Add("Text",
                                                 territoryBindingSource,
                                                 "TerritoryID");

            int    empID = Convert.ToInt32(employeeIDComboBox.Text);
            string terID = territoryIDComboBox.Text;

            var employee = db.Employees.Where(emp => emp.EmployeeID == empID).Single() as Employee;
            var terr     = db.Territories.Where(t => t.TerritoryID == terID).Single() as Territory;

            employee.Territories.Add(terr);
            db.SaveChanges();

            MessageBox.Show("Save!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

            Close();
        }
Exemplo n.º 3
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            List <int> orderID = new List <int>();

            if (dvrgEmp.SelectedRows.Count > 0)
            {
                int empID = (int)dvrgEmp.SelectedRows[0].Cells[0].Value;

                if (MessageBox.Show("Are you sure you want to delete the employee " + empID + "? By deleting an employee, you delete all Orders and Territories associated with it!!!", "Delete Employee", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    var q = from ord in db.Orders
                            where ord.EmployeeID == empID
                            select new
                    {
                        ordID = ord.OrderID
                    };

                    foreach (var i in q)
                    {
                        orderID.Add(i.ordID);
                    }

                    foreach (int j in orderID)
                    {
                        bool a = true;
                        while (a)
                        {
                            try
                            {
                                var od = db.Order_Details.Where(ordDet => ordDet.OrderID == j).First() as Order_Detail;
                                db.Order_Details.Remove(od);
                                db.SaveChanges();
                            }
                            catch
                            {
                                a = false;
                            }
                        }

                        var o = db.Orders.Where(ord => ord.OrderID == j).Single() as Order;
                        db.Orders.Remove(o);
                        db.SaveChanges();
                    }

                    var entity = db.Employees.Where(emp => emp.EmployeeID == empID).Single() as Employee;

                    var terr = entity.Territories.ToList();

                    foreach (var i in terr)
                    {
                        entity.Territories.Remove(i);
                        db.SaveChanges();
                    }


                    (from e2 in db.Employees
                     where e2.ReportsTo == empID
                     select e2).ToList().ForEach(x => x.ReportsTo = null);

                    db.SaveChanges();


                    db.Employees.Remove(entity);
                    db.SaveChanges();
                    employeeBindingSource.DataSource = db.Employees.ToList();
                }
            }
        }