コード例 #1
0
        private void saveBtn_Click(object sender, EventArgs e)
        {
            String name   = BrandNameTxt.Text;
            String id     = BrandIdTxt.Text;
            var    search = (from x in db.LaptopBrands
                             where name == x.LaptopBrandName
                             select x.LaptopBrandName).FirstOrDefault();

            if (name.Length < 2 || name.Length > 15)
            {
                MessageBox.Show("Brand name length must be between 2-15 characters", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (search != null)
            {
                MessageBox.Show("Brand name already exists!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                var searchid = (from x in db.LaptopBrands
                                where x.LaptopBrandID == id
                                select x).FirstOrDefault();
                if (searchid != null)
                {
                    LaptopBrand updateBrand = (from x in db.LaptopBrands
                                               where x.LaptopBrandID == id
                                               select x).FirstOrDefault();
                    updateBrand.LaptopBrandName = name;
                }
                else
                {
                    LaptopBrand insertBrand = new LaptopBrand()
                    {
                        LaptopBrandID   = id,
                        LaptopBrandName = name
                    };
                    db.LaptopBrands.Add(insertBrand);
                }
                db.SaveChanges();
                refresh();
                MessageBox.Show("Data inserted/updated succesfully", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                state();
            }
        }
コード例 #2
0
        private void deleteBtn_Click(object sender, EventArgs e)
        {
            var id   = BrandIdTxt.Text;
            var name = BrandNameTxt.Text;

            if (id == "" || name == "")
            {
                MessageBox.Show("Please select data first!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
                    System.Windows.Forms.DialogResult.Yes)
                {
                    LaptopBrand deleteBrand = (from x in db.LaptopBrands
                                               where x.LaptopBrandID == id
                                               select x).FirstOrDefault();
                    db.LaptopBrands.Remove(deleteBrand);
                    db.SaveChanges();
                    refresh();
                }
                state();
            }
        }