コード例 #1
0
ファイル: formAddNewResource.cs プロジェクト: tyc-4/QR1-15-5
        private void button1_Click(object sender, EventArgs e)
        {
            //add resource data checks
            using (var db = new Session1Entities())
            {
                var itemlist = from x in db.Resources
                               select x.resName;
                if (itemlist.Contains(tbResname.Text))
                {
                    MessageBox.Show("Resource already exist");
                }
                else
                {
                    var qcheck = int.TryParse(textBox1.Text, out int quantity);
                    if (!qcheck || quantity < 0)
                    {
                        MessageBox.Show("Invalid input for quantity");
                    }
                    else
                    {
                        if (quantity == 0 && checkedListBox1.CheckedItems.Count > 0)
                        {
                            MessageBox.Show("You cannot assign items without any quantity to skills");
                        }
                        else
                        {
                            var res = new Resource();
                            res.resName           = tbResname.Text;
                            res.resTypeIdFK       = comboBox1.SelectedIndex + 1;
                            res.remainingQuantity = quantity;

                            db.Resources.Add(res);
                            db.SaveChanges();

                            foreach (string skill in checkedListBox1.CheckedItems)
                            {
                                var skillid = (from x in db.Skills
                                               where x.skillName == skill
                                               select x.skillId).First();
                                var alloc = new Resource_Allocation();
                                alloc.resIdFK   = res.resId;
                                alloc.skillIdFK = skillid;

                                db.Resource_Allocation.Add(alloc);
                            }

                            db.SaveChanges();
                            MessageBox.Show("add success");
                            var nform = new formResourceManagement();
                            nform.Show();
                            this.Close();
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: formUpdateResource.cs プロジェクト: tyc-4/QR1-15-5
        private void button1_Click(object sender, EventArgs e)
        {
            using (var db = new Session1Entities())
            {
                var qcheck = int.TryParse(tbQty.Text, out int quantity);
                if (!qcheck || quantity < 0)
                {
                    MessageBox.Show("Invalid input for quantity");
                }
                else
                {
                    if (quantity == 0 && checkedListBox1.CheckedItems.Count > 0)
                    {
                        MessageBox.Show("You cannot assign items without any quantity to skills");
                    }
                    else
                    {
                        var res = (from x in db.Resources
                                   where x.resId == resid
                                   select x).First();

                        res.remainingQuantity = quantity;

                        var allocO = from x in db.Resource_Allocation
                                     where x.resIdFK == resid
                                     select x;
                        foreach (var a in allocO)
                        {
                            db.Resource_Allocation.Remove(a);
                        }
                        db.SaveChanges();

                        foreach (string skill in checkedListBox1.CheckedItems)
                        {
                            var skillid = (from x in db.Skills
                                           where x.skillName == skill
                                           select x.skillId).First();
                            var alloc = new Resource_Allocation();
                            alloc.resIdFK   = res.resId;
                            alloc.skillIdFK = skillid;

                            db.Resource_Allocation.Add(alloc);
                        }

                        db.SaveChanges();

                        MessageBox.Show("update success");
                        var nform = new formResourceManagement();
                        nform.Show();
                        this.Close();
                    }
                }
            }
        }