private void UpdateBtn_Click(object sender, EventArgs e) { using (var db = new Session1Entities()) { var query = db.Resources.Where(x => x.resId == IDs).FirstOrDefault(); query.remainingQuantity = (int)quantity.Value; if (skills.Visible == false) { try { db.SaveChanges(); MessageBox.Show("Successful!"); this.Hide(); ResouceManagement resouceManagement = new ResouceManagement(); resouceManagement.Show(); } catch (Exception ES) { MessageBox.Show(ES.ToString()); } } else { var query2 = db.Resource_Allocation.Where(x => x.resIdFK == IDs).ToList(); foreach (var item in query2) { db.Resource_Allocation.Remove(item); } for (int i = 0; i < skills.CheckedItems.Count; i++) { Resource_Allocation resource_Allocation = new Resource_Allocation(); resource_Allocation.resIdFK = IDs; resource_Allocation.skillIdFK = i + 1; db.Resource_Allocation.Add(resource_Allocation); } try { db.SaveChanges(); MessageBox.Show("Successful!"); this.Hide(); ResouceManagement resouceManagement = new ResouceManagement(); resouceManagement.Show(); } catch (Exception es) { MessageBox.Show(es.ToString()); } } } }
private void DelBtn_Click(object sender, EventArgs e) { foreach (DataGridViewRow dr in dataGridView1.SelectedRows) { using (var db = new Session1Entities()) { var ID = int.Parse(dr.Cells["ID"].Value.ToString()); var query = db.Resources.Where(x => x.resId == ID).FirstOrDefault(); db.Resources.Remove(query); var query2 = db.Resource_Allocation.Where(x => x.resIdFK == ID).ToList(); foreach (var item in query2) { db.Resource_Allocation.Remove(item); } try { db.SaveChanges(); MessageBox.Show("Deleted Successfully!"); var q = db.Resources.ToList(); dataGridView1.DataSource = CDT(q); dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.Columns["ID"].Visible = false; foreach (DataGridViewRow datar in dataGridView1.Rows) { if (datar.Cells["Available Quantity"].ToString() == "Not Available") { datar.DefaultCellStyle.BackColor = Color.Red; } } } catch (Exception es) { MessageBox.Show(es.ToString()); } } } }
private void CABtn_Click(object sender, EventArgs e) { using (var db = new Session1Entities()) { User user = new User(); user.userName = UName.Text; if (UID.TextLength < 8) { MessageBox.Show("User ID must have a minimum of 8 characters!"); return; } else { var query2 = db.Users.Where(x => x.userId == UID.Text).FirstOrDefault(); if (query2 != null) { MessageBox.Show("User ID already exists!"); } else { user.userId = UID.Text; } } //Checking password if (Pass.TextLength == 0) { MessageBox.Show("Password must have a value!"); return; } if (PassC.TextLength == 0) { MessageBox.Show("Confirm Password must have a value!"); return; } if (Pass.Text != PassC.Text) { MessageBox.Show("Passwords do not match!"); } else { user.userPw = Pass.Text; } var ID = UType.SelectedIndex + 1; var query = db.User_Type.Where(x => x.userTypeId == ID).FirstOrDefault(); user.userTypeIdFK = query.userTypeId; try { db.Users.Add(user); db.SaveChanges(); MessageBox.Show("Account Created Successfully!"); this.Hide(); Form1 form = new Form1(); form.ShowDialog(); }catch (Exception es) { MessageBox.Show(es.ToString()); } } }
private void createBtn_Click(object sender, EventArgs e) { using (var context = new Session1Entities()) { //Checks if User ID is at least 8 characters long, else error message if (userIdBox.TextLength < 8) { MessageBox.Show("ID needs to be at least 8 characters"); } //Checks if password fields are the same, else error message else if (passwordBox.Text != rePasswordBox.Text) { MessageBox.Show("Passwords must be the same!"); } //Checks if User's type is selected, else error message else if (typeUserBox.SelectedItem == null) { MessageBox.Show("Please choose user type!"); } else { //Check if User ID exist in the Database. If ID exist, prompt user an error message. //If not, allow creation of new account var checkUserID = (from x in context.Users where x.userId == userIdBox.Text select x).FirstOrDefault(); if (checkUserID == null) { var getUserTypeID = (from x in context.User_Type where x.userTypeName == typeUserBox.SelectedItem.ToString() select x.userTypeId).First(); context.Users.Add(new User() { userName = userNameBox.Text, userId = userIdBox.Text, userPw = passwordBox.Text, userTypeIdFK = getUserTypeID }); context.SaveChanges(); this.Hide(); (new Main()).ShowDialog(); this.Close(); } else { MessageBox.Show("User ID already exist! Please choose another ID!", "Used ID", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } }
/// <summary> /// This method is called when the delete button is triggered /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void deleteBtn_Click(object sender, EventArgs e) { //Checks if they is a selected row, else prompts user to select a row to delete if (dataGridView1.CurrentRow == null) { MessageBox.Show("Please select a resource to delete!", "No resource selected", MessageBoxButtons.OK, MessageBoxIcon.Error); } //Runs when VadCheck at If clause is passed else { var getResourceID = Convert.ToInt32(dataGridView1.CurrentRow.Cells[5].Value); using (var context = new Session1Entities()) { var getResourceDelete = (from x in context.Resources where x.resId == getResourceID select x).First(); var getAllocationDelete = (from x in context.Resource_Allocation where x.resIdFK == getResourceID select x); context.Resources.Remove(getResourceDelete); foreach (var item in getAllocationDelete) { context.Resource_Allocation.Remove(item); } context.SaveChanges(); var getRowToDelete = dataGridView1.CurrentRow.Index; dataGridView1.Rows.RemoveAt(getRowToDelete); } } }
private void Update_Load(object sender, EventArgs e) { using (var context = new Session1Entities()) { #region Populating Allocation Checklist Box var getSkills = (from x in context.Skills select x.skillName); var skills = new HashSet <string>(); foreach (var item in getSkills) { skills.Add(item); } clbAllocation.Items.AddRange(skills.ToArray()); #endregion #region Initialising label text for selected resources var getResourceName = (from x in context.Resources where x.resId == _resID select x.resName).First(); lblResourceName.Text = getResourceName; var getResourceType = (from x in context.Resources where x.resId == _resID select x.Resource_Type.resTypeName).First(); lblResourceType.Text = getResourceType; var getResourceAmount = (from x in context.Resources where x.resId == _resID select x.remainingQuantity).First(); txtQuantityBox.Text = getResourceAmount.ToString(); #endregion #region Auto check skills that were previously allocated to for selected resource var checkAllocatedSkills = (from x in context.Resource_Allocation where x.resIdFK == _resID select x).FirstOrDefault(); if (checkAllocatedSkills != null) { var getAllocatedSkills = (from x in context.Resource_Allocation where x.resIdFK == _resID select x.Skill.skillName); foreach (var item in getAllocatedSkills) { _list.Add(item); clbAllocation.SetItemChecked(clbAllocation.Items.IndexOf(item), true); } var getAllocatedSkillsToDelete = (from x in context.Resource_Allocation where x.resIdFK == _resID select x).ToList(); foreach (var item in getAllocatedSkillsToDelete) { context.Resource_Allocation.Remove(item); context.SaveChanges(); } } #endregion } }
/// <summary> /// This event is triggered when the update button is clicked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnUpdate_Click(object sender, EventArgs e) { using (var context = new Session1Entities()) { //Check if quantity is empty if (txtQuantityBox.Text == null) { MessageBox.Show("Please check your entries again!", "Empty Field(s)", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } //Check if quantity is more than 0 but no allocation of skill is made else if (Int32.Parse(txtQuantityBox.Text) > 0 && clbAllocation.CheckedItems.Count == 0) { MessageBox.Show("Resource must have at least 1 allocated skill!", "Allocation of resource not set", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } //Check if quantity is invalid as in negative / less than 0 else if (Int32.Parse(txtQuantityBox.Text) < 0) { MessageBox.Show("Resource amount cannot be negative!", "Invalid Amount", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { //Check if quantity is 0 but there is allocation(s) of skill if (Int32.Parse(txtQuantityBox.Text) == 0 && clbAllocation.CheckedItems.Count > 0) { MessageBox.Show("Resource cannot be allocated if amount is 0", "Unable to allocate resource", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } //If quantity is 0 and no allocation checks out, update amount to DB else if (Int32.Parse(txtQuantityBox.Text) == 0 && clbAllocation.CheckedItems.Count == 0) { var updateResource = (from x in context.Resources where x.resId == _resID select x).First(); updateResource.remainingQuantity = Int32.Parse(txtQuantityBox.Text); } //Else update everything into DB else if (Int32.Parse(txtQuantityBox.Text) > 0) { var updateResource = (from x in context.Resources where x.resId == _resID select x).First(); updateResource.remainingQuantity = Int32.Parse(txtQuantityBox.Text); if (_list.Count() != 0) { foreach (var item in _list) { var getSkillID = (from x in context.Skills where x.skillName == item select x.skillId).First(); context.Resource_Allocation.Add(new Resource_Allocation() { resIdFK = _resID, skillIdFK = getSkillID }); } } } context.SaveChanges(); this.Hide(); (new ResourceManagement()).ShowDialog(); this.Close(); } } }
private void AddBtn_Click(object sender, EventArgs e) { using (var db = new Session1Entities()) { Resource resource = new Resource(); var query = db.Resources.Where(x => x.resName == name.Text).FirstOrDefault(); if (query != null) { MessageBox.Show("Resource Name is already used!"); return; } else { resource.resName = name.Text; resource.resTypeIdFK = type.SelectedIndex + 1; resource.remainingQuantity = (int)quantity.Value; try { db.Resources.Add(resource); if (skills.CheckedItems.Count > 0) { foreach (var item in skills.CheckedItems) { var name = item.ToString(); var val = db.Skills.Where(x => x.skillName == name).FirstOrDefault(); var ID = resource.resId; Resource_Allocation resource_ = new Resource_Allocation(); resource_.resIdFK = ID; resource_.skillIdFK = val.skillId; RA.Add(resource_); } try { if (RA.Count == 0) { var ID = resource.resId; Resource_Allocation resource_ = new Resource_Allocation(); resource_.resIdFK = ID; db.Resource_Allocation.Add(resource_); } foreach (var item in RA) { db.Resource_Allocation.Add(item); } db.SaveChanges(); MessageBox.Show("Created Successfully!"); this.Hide(); } catch (Exception es) { MessageBox.Show(es.ToString()); } } else { var ID = resource.resId; Resource_Allocation resource_ = new Resource_Allocation(); resource_.resIdFK = ID; db.Resource_Allocation.Add(resource_); try { db.SaveChanges(); }catch (Exception es) { MessageBox.Show(es.ToString()); } MessageBox.Show("Created Successfully!"); this.Hide(); } } catch (Exception es) { MessageBox.Show(es.ToString()); } } } }
/// <summary> /// This method is called when the Add button is triggered /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void addBtn_Click(object sender, EventArgs e) { using (var context = new Session1Entities()) { var checkResourceExistence = (from x in context.Resources where x.resName == resourceNameBox.Text select x).FirstOrDefault(); var getNewID = (from x in context.Resources orderby x.resId descending select x.resId).First() + 1; //Check if intended new resource has a record in the database if (checkResourceExistence != null) { MessageBox.Show("Resource already exist in Database!", "Duplicate Resource", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { //Check if the Type of Resource and and quantity is selected and keyed in properly if (typeBox.SelectedItem == null || quantityBox.Text == null) { MessageBox.Show("Please check your entries again!", "Empty Field(s)", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } //Check if there is any allocation if quantity is more than 0. Resource must have an allocation if quantity is more than 0 else if (Int32.Parse(quantityBox.Text) > 0 && allocationBox.CheckedItems.Count == 0) { MessageBox.Show("Resource must have at least 1 allocated skill!", "Allocation of resource not set", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } //Check if user keyed in an invalid integer else if (Int32.Parse(quantityBox.Text) < 0) { MessageBox.Show("Resource amount cannot be negative!", "Invalid Amount", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } //Check if there are any allocation if the quantity is set to 0 else if (Int32.Parse(quantityBox.Text) == 0 && allocationBox.CheckedItems.Count > 0) { MessageBox.Show("Resource cannot be allocated if amount is 0", "Unable to allocate resource", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { //If quantity is 0, add everything keyed in except for skill allocation to DB if (Int32.Parse(quantityBox.Text) == 0) { var getTypeID = (from x in context.Resource_Type where x.resTypeName == typeBox.SelectedItem.ToString() select x.resTypeId).First(); context.Resources.Add(new Resource() { resId = getNewID, resName = resourceNameBox.Text, remainingQuantity = Int32.Parse(quantityBox.Text), resTypeIdFK = getTypeID }); } //If quantity more 0, add everything keyed in to DB else if (Int32.Parse(quantityBox.Text) > 0) { var getTypeID = (from x in context.Resource_Type where x.resTypeName == typeBox.SelectedItem.ToString() select x.resTypeId).First(); context.Resources.Add(new Resource() { resId = getNewID, resName = resourceNameBox.Text, remainingQuantity = Int32.Parse(quantityBox.Text), resTypeIdFK = getTypeID }); if (list.Count() != 0) { foreach (var item in list) { var getSkillID = (from x in context.Skills where x.skillName == item select x.skillId).First(); context.Resource_Allocation.Add(new Resource_Allocation() { resIdFK = getNewID, skillIdFK = getSkillID }); } } } context.SaveChanges(); resourceNameBox.Text = ""; typeBox.SelectedItem = null; quantityBox.Text = ""; allocationBox.SelectedItems.Clear(); list.Clear(); } } } }