Exemplo n.º 1
0
 private void btnUpdate_Click(object sender, EventArgs e)
 {
     using (var context = new Session1Entities())
     {
         var getAllocation = (from x in context.Resource_Allocation
                              where x.resIdFK == _resourceID
                              select x);
         foreach (var item in getAllocation)
         {
             context.Resource_Allocation.Remove(item);
         }
         context.SaveChanges();
         var getResource = (from x in context.Resources
                            where x.resId == _resourceID
                            select x).FirstOrDefault();
         getResource.remainingQuantity = Convert.ToInt32(nudAvailableQuantity.Value);
         context.SaveChanges();
         foreach (var item in _list)
         {
             var getSkillID = (from x in context.Skills
                               where x.skillName == item
                               select x.skillId).FirstOrDefault();
             context.Resource_Allocation.Add(new Resource_Allocation()
             {
                 resIdFK = _resourceID, skillIdFK = getSkillID
             });
         }
         context.SaveChanges();
         MessageBox.Show("Updated resource successfully!", "Update Resource", MessageBoxButtons.OK, MessageBoxIcon.Information);
         this.Hide();
         (new ResourceManagement()).ShowDialog();
         this.Close();
     }
 }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show("Please select a resource to delete!", "Delete Resource", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                var response = MessageBox.Show("Are you sure you want to delete this resource?", "Delete Resource", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (response == DialogResult.Yes)
                {
                    var resourceID = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
                    using (var context = new Session1Entities())
                    {
                        var allocation = (from x in context.Resource_Allocation
                                          where x.resIdFK == resourceID
                                          select x);
                        foreach (var item in allocation)
                        {
                            context.Resource_Allocation.Remove(item);
                        }
                        context.SaveChanges();

                        var resourceDelete = (from x in context.Resources
                                              where x.resId == resourceID
                                              select x).FirstOrDefault();
                        context.Resources.Remove(resourceDelete);
                        context.SaveChanges();
                    }
                    MessageBox.Show("Resource deleted successfully!", "Delete Resource", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                }
            }
        }
 private void btnAdd_Click(object sender, EventArgs e)
 {
     using (var context = new Session1Entities())
     {
         var checkResource = (from x in context.Resources
                              where x.resName == txtResourceName.Text
                              select x).FirstOrDefault();
         if (checkResource != null)
         {
         }
         else if (cbResourceType.SelectedItem == null)
         {
         }
         else
         {
             var getResourceTypeID = (from x in context.Resource_Type
                                      where x.resTypeName == cbResourceType.SelectedItem.ToString()
                                      select x.resTypeId).FirstOrDefault();
             context.Resources.Add(new Resource()
             {
                 resName           = txtResourceName.Text,
                 remainingQuantity = Convert.ToInt32(nudAvailableQuantity.Value),
                 resTypeIdFK       = getResourceTypeID
             });
             context.SaveChanges();
             var getNewID = (from x in context.Resources
                             where x.resName == txtResourceName.Text
                             select x.resId).FirstOrDefault();
             foreach (var item in _list)
             {
                 var getSkillID = (from x in context.Skills
                                   where x.skillName == item
                                   select x.skillId).FirstOrDefault();
                 context.Resource_Allocation.Add(new Resource_Allocation()
                 {
                     resIdFK = getNewID, skillIdFK = getSkillID
                 });
             }
             context.SaveChanges();
             MessageBox.Show("Successfully added new resource!", "Add Resource", MessageBoxButtons.OK, MessageBoxIcon.Information);
             txtResourceName.Text       = string.Empty;
             nudAvailableQuantity.Value = 0;
             AddNewResource_Load(null, null);
         }
     }
 }
 private void btnCreate_Click(object sender, EventArgs e)
 {
     using (var context = new Session1Entities())
     {
         var checkUserID = (from x in context.Users
                            where x.userId == txtUserID.Text
                            select x).FirstOrDefault();
         if (txtUserID.TextLength < 8)
         {
             MessageBox.Show("User ID must be 8 characters long or more!", "Create Account", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         else if (cbUserType.SelectedItem == null)
         {
             MessageBox.Show("Please select User Type!", "Create Account", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         else if (checkUserID != null)
         {
             MessageBox.Show("User ID has been taken!", "Create Account", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         else if (txtPassword.Text != txtReEnterPassword.Text)
         {
             MessageBox.Show("Passwords do not match! Please check your passwords again!", "Create Account", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         else
         {
             var getUserTypeID = (from x in context.User_Type
                                  where x.userTypeName == cbUserType.SelectedItem.ToString()
                                  select x.userTypeId).FirstOrDefault();
             context.Users.Add(new User()
             {
                 userId       = txtUserID.Text,
                 userName     = txtUserName.Text,
                 userPw       = txtReEnterPassword.Text,
                 userTypeIdFK = getUserTypeID
             });
             context.SaveChanges();
             MessageBox.Show("Account created successfully!", "Create Account", MessageBoxButtons.OK, MessageBoxIcon.Information);
             this.Close();
         }
     }
 }