Esempio n. 1
0
 private void btnLuu_Click(object sender, EventArgs e)
 {
     #region Validation data
     int   intSoLuong;
     bool  isInt = int.TryParse(txtSoLuong.Text.Trim().ToString(), out intSoLuong);
     float floatDonGiaNhap;
     bool  isFloatNhap = float.TryParse(txtDonGiaNhap.Text.Trim().ToString(), out floatDonGiaNhap);
     float floatDonGiaBan;
     bool  isFloatBan = float.TryParse(txtDonGiaBan.Text.Trim().ToString(), out floatDonGiaBan);
     if (txtTenHang.Text.Trim().Length == 0)
     {
         MessageBox.Show("Bạn phải nhập tên hàng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return;
     }
     else if (!isInt || int.Parse(txtSoLuong.Text) < 0)
     {
         MessageBox.Show("Bạn phải nhập số lượng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return;
     }
     else if (!isFloatNhap || float.Parse(txtDonGiaNhap.Text) < 0)
     {
         MessageBox.Show("Bạn phải nhập giá nhập", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return;
     }
     else if (!isFloatBan || float.Parse(txtDonGiaBan.Text) < 0)
     {
         MessageBox.Show("Bạn phải nhập giá bán", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return;
     }
     else if (pbHinh.Image == null) // Kiểm tra phải nhập hình
     {
         MessageBox.Show("Bạn phải upload hình", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
         return;
     }
     #endregion
     else
     {
         string nameImage = string.Empty;
         try
         {
             //Get name image
             nameImage = txtHinh.Text.Substring(txtHinh.Text.LastIndexOf('\\') + 2);
             pbHinh.Image.Save(Path.Combine(Directory.GetCurrentDirectory(), "Images", nameImage));
             DTO_SanPham h = new DTO_SanPham(
                 txtTenHang.Text,
                 int.Parse(txtSoLuong.Text),
                 float.Parse(txtDonGiaNhap.Text),
                 float.Parse(txtDonGiaBan.Text),
                 Path.Combine("Images", nameImage),
                 txtGhiChu.Text, stremail);
             if (busHang.InsertDataSanPham(h))
             {
                 MessageBox.Show("Thêm sản phẩm thành công");
                 //File.Copy(fileAddress, fileSavePath, true);
                 ResetValues();
                 LoadGridView_Hang();
             }
             else
             {
                 MessageBox.Show("Thêm sản phẩm không thành công");
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
     }
 }