private void btLuuNV_Click(object sender, EventArgs e) { string email; int role = 0; if (rbRoleQL.Checked) { role = 1; } if (txtTenNV.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập tên", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtTenNV.Focus(); return; } else if (txtSdtNV.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập số điện thoại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtSdtNV.Focus(); return; } if (txtEmailNV.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập email", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtEmailNV.Focus(); return; } else if (!IsValid(txtEmailNV.Text.Trim())) { MessageBox.Show("Bạn phải nhập đúng định dạng email", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtEmailNV.Focus(); return; } if (txtDiachiNV.Text.Trim().Length == 0) { MessageBox.Show("Bạn phải nhập địa chỉ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtDiachiNV.Focus(); return; } if (rbRoleQL.Checked == false && rbRoleNV.Checked == false) { MessageBox.Show("Bạn phải chọn vai trò", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txtTenNV.Focus(); return; } else { DTO_NHANVIEN nv = new DTO_NHANVIEN(txtTenNV.Text, txtSdtNV.Text, txtEmailNV.Text, txtDiachiNV.Text, role); if (busNhanVien.insertNhanVien(nv)) { MessageBox.Show("Thêm thành công"); ResetValues(); LoadGridview_NhanVien(); email = txtEmailNV.Text; } else { MessageBox.Show("Thêm không thành công"); } } }
private void btn_luu_Click(object sender, EventArgs e) { try { var ktraEmail = from n in db.NHANVIENs select n.Email; foreach (var item in ktraEmail) { if (txt_emailNhanVien.Text == item) { MessageBox.Show("Email đã tồn tại vui lòng thử lại "); return; } } int Role = 0;//role = 0 là nhân viên if (rad_quanTri.Checked) { Role = 1; } int tinhTrang = 0;// tình trạng = 0 là không hoạt động if (rad_hoatDong.Checked) { tinhTrang = 1; } if (txt_emailNhanVien.Text.Trim().Length == 0)//kiểm tra email đã nhập chưa { MessageBox.Show("Vui Lòng Nhập Email", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txt_emailNhanVien.Focus(); return; } else if (!IsValid(txt_emailNhanVien.Text.Trim())) { MessageBox.Show("Vui Lòng Nhập Email", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (txt_TenNhanVien.Text.Trim().Length == 0) { MessageBox.Show("Vui Lòng Nhập Tên Nhân Viên", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txt_TenNhanVien.Focus(); return; } else if (txt_DiaChiNhanVien.Text.Trim().Length == 0) { MessageBox.Show("Vui Lòng Nhập Địa Chỉ Nhân Viên", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information); txt_DiaChiNhanVien.Focus(); return; } else if (rad_hoatDong.Checked == false && rad_khongHoatDong.Checked == false) { MessageBox.Show("Vui Lòng Chọn Tình Trạng Tài Khoản", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else if (rad_nhanVien.Checked == false && rad_quanTri.Checked == false) { MessageBox.Show("Vui Lòng Chọn Chức Vụ", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { DTO_NHANVIEN nv = new DTO_NHANVIEN(txt_emailNhanVien.Text, txt_TenNhanVien.Text, txt_DiaChiNhanVien.Text, Role, tinhTrang); if (BUS_NHANVIEN.insertNhanVien(nv)) { MessageBox.Show("Thêm Nhân Viên Thành Công!"); ResetValue(); LoadGridView_NhanVien(); sendMail(nv.Email); } else { MessageBox.Show("Thêm Không Thành Công!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }