示例#1
0
        private async void btnLuuKieuSP_Click(object sender, EventArgs e)
        {
            using (GalaxyMobileEntities db = new GalaxyMobileEntities())
            {
                ChiTietSP sp = new ChiTietSP();
                try
                {
                    // sp.Anh = CovertImageToBinary(picBoxKieuSP.Image);
                    sp.Anh = CovertImageToBinary(picBoxKieuSP.BackgroundImage);
                }
                catch
                {
                }
                sp.Gia       = 0;
                sp.SoluongSP = 0;
                sp.MaKieu    = textBoxMaKieu.Text;
                sp.MaSP      = /*textBoxMaSP.Text;*/ cmBoxMaSP.SelectedValue.ToString();
                sp.MaMau     = cmBoxMauSP.SelectedValue.ToString();
                try
                {
                    if (NewCTSP)
                    {
                        db.ChiTietSPs.Add(sp);
                        db.SaveChanges();
                        //    await db.SaveChangesAsync();
                        MessageBox.Show("Thêm Thành Công!", "Thông Báo");

                        ChiTietSPBUS.Them_CTSPMoi_Into_KhoHang_(sp.MaKieu);
                    }
                    else
                    {
                        try
                        {
                            // sp.Anh = CovertImageToBinary(picBoxKieuSP.Image);
                            sp.Anh = CovertImageToBinary(picBoxKieuSP.BackgroundImage);
                        }
                        catch
                        {
                            sp.Anh = ChiTietSPBUS.LayAnhKieuSP(cmBoxMaKieuSP.SelectedValue.ToString(), cmBoxMaSP.SelectedValue.ToString());
                        }

                        int r = dgvCTSP.CurrentCell.RowIndex;
                        //sp.Gia = Convert.ToDecimal(dgvCTSP.Rows[r].Cells[3].Value.ToString());
                        sp.Gia       = Convert.ToDecimal(txtBoxGiaSP.Text);
                        sp.SoluongSP = Convert.ToInt32(txtboxSoLuongAll.Text);
                        db.ChiTietSPs.Attach(sp);
                        db.Entry(sp).State = System.Data.Entity.EntityState.Modified;
                        await db.SaveChangesAsync();

                        MessageBox.Show("Thay Đổi Thành Công!", "Thông Báo");
                        string id = cmBoxMaSP.SelectedValue.ToString();
                        chiTietSPBindingSource.DataSource = ChiTietSPBUS.GetChiTietSPByIDSP(id);
                    }
                }
                catch { MessageBox.Show("Không Thể Thực Hiện Thao Tác!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            }
            btnLuuKieuSP.Visible      = false;
            btnHuyKieuSP.Visible      = false;
            btnThemKieuSP.Visible     = true;
            btnChinhSuaKieuSP.Visible = true;
            btnXoaKieuSP.Visible      = true;
            textBoxMaKieu.ReadOnly    = false;
            textBoxMaSP.ReadOnly      = false;
            textBoxMaKieu.Visible     = false;
            textBoxMaSP.Visible       = false;
            txtBoxGiaSP.ReadOnly      = true;
            textBoxMau.Visible        = false;
            dgvCTSP.Enabled           = true;
        }