Пример #1
0
 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;
                        }
                    }
                }
            }
        }