partial void DeleteHANG_HOA(HANG_HOA instance);
 partial void InsertHANG_HOA(HANG_HOA instance);
 partial void UpdateHANG_HOA(HANG_HOA instance);
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (!Check())
            {
                return;
            }

            try
            {
                if (tinh_trang == "them")
                {
                    HANG_HOA hh = new HANG_HOA
                    {
                        Ma_Hang = txtMaHang.Text,
                        Ten_Hang = txtTenHang.Text,
                        Nhom_Hang = string.IsNullOrEmpty(txtNhomHang.Text) ? "" : txtNhomHang.EditValue.ToString(),
                        Don_Vi = txtDonVi.Text,
                        Gia_Nhap = txtGiaNhapKho.Value,
                        Gia_Xuat = txtGiaXuatKho.Value,
                        Ghi_Chu = txtGhiChu.Text
                    };

                    if (picAnh.Image != null)
                    {
                        Image anh = picAnh.Image;
                        MemoryStream ms = new MemoryStream();
                        anh.Save(ms, anh.RawFormat);
                        byte[] anh_Bytes = ms.GetBuffer();
                        ms.Close();

                        hh.Anh = anh_Bytes;
                    }

                    db.HANG_HOAs.InsertOnSubmit(hh);
                }
                else
                {
                    var hh = (from h in db.HANG_HOAs
                              where h.Ma_Hang == txtMaHang.Text
                              select h).FirstOrDefault();

                    hh.Ma_Hang = txtMaHang.Text;
                    hh.Ten_Hang = txtTenHang.Text;
                    hh.Nhom_Hang = string.IsNullOrEmpty(txtNhomHang.Text) ? "" : txtNhomHang.EditValue.ToString();
                    hh.Don_Vi = txtDonVi.Text;
                    hh.Gia_Nhap = txtGiaNhapKho.Value;
                    hh.Gia_Xuat = txtGiaXuatKho.Value;
                    hh.Ghi_Chu = txtGhiChu.Text;

                    if (picAnh.Image != null)
                    {
                        Image anh = picAnh.Image;
                        MemoryStream ms = new MemoryStream();
                        anh.Save(ms, anh.RawFormat);
                        byte[] anh_Bytes = ms.GetBuffer();
                        ms.Close();

                        hh.Anh = anh_Bytes;
                    }
                    else
                    {
                        hh.Anh = null;
                    }
                }

                db.SubmitChanges();
                RaiseReloadEventHander();
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }