private void btnUpdate_Click(object sender, EventArgs e) { using (var db = new Session1_WSOHEntities()) { var currentResource = db.Resources.Where(a => a.resId == resId).FirstOrDefault(); var qty = Convert.ToInt32(nudAvailableQty.Value); currentResource.remainingQuantity = qty; foreach (var skill in currentResource.Resource_Allocation.ToList()) { db.Resource_Allocation.Remove(skill); } foreach (var updatedSkill in clbAllocatedSkills.CheckedItems) { var skillId = db.Skills.Where(a => a.skillName == updatedSkill.ToString()).Select(a => a.skillId).FirstOrDefault(); var newAllocatedResources = new Resource_Allocation() { resIdFK = resId, skillIdFK = skillId }; db.Resource_Allocation.Add(newAllocatedResources); } try { db.SaveChanges(); MessageBox.Show("Changes have been successfully made to the resource"); this.Close(); } catch (Exception) { MessageBox.Show("Oops, something went wrong while saving your resource changes"); throw; } } }
private void btnAddresource_Click(object sender, EventArgs e) { var resourceName = tbResname.Text; var resourceType = ((Resource_Type)cbResType.SelectedItem).resTypeId; var availableQty = Convert.ToInt32(nudAvailableQty.Value); if (resourceName.Length == 0) { MessageBox.Show("Please enter a valid Resource name"); } else { using (var db = new Session1_WSOHEntities()) { //check if the resource exists first var checkResource = db.Resources.Where(a => a.resName == resourceName).FirstOrDefault(); if (checkResource != null) { MessageBox.Show("Sorry, this item exists in the records already"); } else { var newResource = new Resource() { resName = resourceName, resTypeIdFK = resourceType, remainingQuantity = availableQty }; db.Resources.Add(newResource); foreach (var selectedSkill in clbAllocatedSkills.CheckedItems) { var skillName = (string)selectedSkill; var skillid = db.Skills.Where(a => a.skillName == skillName).Select(a => a.skillId).FirstOrDefault(); var newResAloc = new Resource_Allocation() { resIdFK = newResource.resId, skillIdFK = skillid }; db.Resource_Allocation.Add(newResAloc); } try { db.SaveChanges(); MessageBox.Show("New Resource successfully created and saved to database"); tbResname.Clear(); nudAvailableQty.Value = 0; this.AddResource_Load(sender, e); } catch (Exception) { MessageBox.Show("Oops, an error occured"); throw; } } } } }
private void btnCreateAcct_Click(object sender, EventArgs e) { var selectedUserType = ((User_Type)cbUserType.SelectedItem).userTypeId; if (tbUsername.Text.Length != 0 || tbUserID.Text.Length != 0 || tbPassword.Text.Length != 0 || tbRtpassword.Text.Length != 0) { if (tbUserID.Text.Length < 8) { MessageBox.Show("User Id must be more than 8 characters in length"); } else { if (tbPassword.Text != tbRtpassword.Text) { MessageBox.Show("Mismatch Passwords"); } else { using (var db = new Session1_WSOHEntities()) { var checkUserId = db.Users.Where(a => a.userId == tbUserID.Text).FirstOrDefault(); if (checkUserId == null) { var newUser = new User() { userId = tbUserID.Text, userName = tbUsername.Text, userPw = tbPassword.Text, userTypeIdFK = selectedUserType }; db.Users.Add(newUser); db.SaveChanges(); this.Close(); } else { MessageBox.Show("Sorry, UserID has been taken"); } } } } } else { MessageBox.Show("Missing Fields"); } }
private void btnDelete_Click(object sender, EventArgs e) { var selectedResource = Convert.ToInt32(dgvResources.CurrentRow.Cells[5].Value); using (var db = new Session1_WSOHEntities()) { var resourceTBD = db.Resources.Where(a => a.resId == selectedResource).FirstOrDefault(); foreach (var r in resourceTBD.Resource_Allocation.ToList()) { db.Resource_Allocation.Remove(r); } db.Resources.Remove(resourceTBD); try { db.SaveChanges(); MessageBox.Show("Resource Successfully deleted"); this.ResourceManagement_Load(sender, e); } catch (Exception) { MessageBox.Show("Whoops! An error occured while deleting this resource"); } } }