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 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> /// Adds a new resource and its allocation, depending on if the checkbox for the different skills are checked. /// </summary> /// <returns></returns> public async Task AddResource() { using (var db = new Session1Entities1()) { //need to add resource and RA entries. int resourceid = 1; try { resourceid = (from x in db.Resources orderby x.resId descending select x.resId).First() + 1; } catch { } var resource = new Resource() { remainingQuantity = (int)numericUpDown1.Value, resId = resourceid, resName = resname_box.Text, resTypeIdFK = restype_combo.SelectedIndex + 1 }; db.Resources.Add(resource); if (CS.Checked) { //try catch because it might error if there are no results found(database does not contain any values by default) try { //try getting the largest id +1 as entity framework does not support auto incrementing. var ra = new Resource_Allocation() { allocId = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1, resIdFK = resourceid, skillIdFK = 1 }; db.Resource_Allocation.Add(ra); } catch { var ra = new Resource_Allocation() { allocId = 1, resIdFK = resourceid, skillIdFK = 1 }; db.Resource_Allocation.Add(ra); } } if (SSB.Checked) { try { var ra = new Resource_Allocation() { allocId = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1, resIdFK = resourceid, skillIdFK = 2 }; db.Resource_Allocation.Add(ra); } catch { var ra = new Resource_Allocation() { allocId = 1, resIdFK = resourceid, skillIdFK = 2 }; db.Resource_Allocation.Add(ra); } } if (NSA.Checked) { try { var ra = new Resource_Allocation() { allocId = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1, resIdFK = resourceid, skillIdFK = 3 }; db.Resource_Allocation.Add(ra); } catch { var ra = new Resource_Allocation() { allocId = 1, resIdFK = resourceid, skillIdFK = 3 }; db.Resource_Allocation.Add(ra); } } if (WT.Checked) { try { var ra = new Resource_Allocation() { allocId = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1, resIdFK = resourceid, skillIdFK = 4 }; db.Resource_Allocation.Add(ra); } catch { var ra = new Resource_Allocation() { allocId = 1, resIdFK = resourceid, skillIdFK = 4 }; db.Resource_Allocation.Add(ra); } } await db.SaveChangesAsync(); MessageBox.Show("Done!"); back_button_Click(null, null); } }
/// <summary> /// Update function, tries to update and check if the resource allocation already exists for the specific resource and will update, add and delete accordingly. /// </summary> /// <param name="id"></param> /// <returns></returns> /// public async Task updateDatabase(int id) { using (var db = new Session1Entities1()) { var res = (from r in db.Resources where r.resId == id select r).First(); var ras = (from ra in db.Resource_Allocation where ra.resIdFK == id select ra.skillIdFK).ToList(); res.remainingQuantity = (int)numericUpDown1.Value; if (CS.Checked && !ras.Contains(1)) { try { var ra = new Resource_Allocation() { allocId = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1, resIdFK = res.resId, skillIdFK = 1 }; db.Resource_Allocation.Add(ra); } catch { var ra = new Resource_Allocation() { allocId = 1, resIdFK = res.resId, skillIdFK = 1 }; db.Resource_Allocation.Add(ra); } } else if (!CS.Checked && ras.Contains(1)) { //sometimes .First() can return an error, hence put it in a try catch try { var findra = (from a in db.Resource_Allocation where a.skillIdFK == 1 where a.resIdFK == id select a).First(); db.Resource_Allocation.Remove(findra); } catch { } } if (SSB.Checked && !ras.Contains(2)) { try { var ra = new Resource_Allocation() { allocId = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1, resIdFK = res.resId, skillIdFK = 2 }; db.Resource_Allocation.Add(ra); } catch { var ra = new Resource_Allocation() { allocId = 1, resIdFK = res.resId, skillIdFK = 2 }; db.Resource_Allocation.Add(ra); } } else if (!SSB.Checked && ras.Contains(2)) { //sometimes .First() can return an error, hence put it in a try catch try { var findra = (from a in db.Resource_Allocation where a.skillIdFK == 2 where a.resIdFK == id select a).First(); db.Resource_Allocation.Remove(findra); } catch { } } if (NSA.Checked && !ras.Contains(3)) { try { var ra = new Resource_Allocation() { allocId = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1, resIdFK = res.resId, skillIdFK = 3 }; db.Resource_Allocation.Add(ra); } catch { var ra = new Resource_Allocation() { allocId = 1, resIdFK = res.resId, skillIdFK = 3 }; db.Resource_Allocation.Add(ra); } } else if (!NSA.Checked && ras.Contains(3)) { //sometimes .First() can return an error, hence put it in a try catch try { var findra = (from a in db.Resource_Allocation where a.skillIdFK == 3 where a.resIdFK == id select a).First(); db.Resource_Allocation.Remove(findra); } catch { } } if (WT.Checked && !ras.Contains(4)) { try { var ra = new Resource_Allocation() { allocId = (from x in db.Resource_Allocation orderby x.allocId descending select x.allocId).First() + 1, resIdFK = res.resId, skillIdFK = 4 }; db.Resource_Allocation.Add(ra); } catch { var ra = new Resource_Allocation() { allocId = 1, resIdFK = res.resId, skillIdFK = 4 }; db.Resource_Allocation.Add(ra); } } else if (!WT.Checked && ras.Contains(4)) { //sometimes .First() can return an error, hence put it in a try catch try { var findra = (from a in db.Resource_Allocation where a.skillIdFK == 4 where a.resIdFK == id select a).First(); db.Resource_Allocation.Remove(findra); } catch { } } await db.SaveChangesAsync(); MessageBox.Show("Done"); } }