private void button5_SaveChange_Click(object sender, EventArgs e) { switch (eTypeManage) { case EnumMyStruct.NguoiDung: SQL.Struct.NguoiDung nd = AddDataToObject(EnumMyStruct.NguoiDung) as SQL.Struct.NguoiDung; if (!SQL.ListData.GetDataFromSQL.Update_NguoiDung(nd)) { MessageBox.Show("Không thể update bản ghi"); } break; case EnumMyStruct.NhanVien: SQL.Struct.NhanVien nv = AddDataToObject(EnumMyStruct.NhanVien) as SQL.Struct.NhanVien; if (!SQL.ListData.GetDataFromSQL.Update_NhanVien(nv)) { MessageBox.Show("Không thể update bản ghi"); } break; default: break; } button2_Reset_Click(sender, e); }
private object AddDataToObject(EnumMyStruct _eType) { switch (_eType) { case EnumMyStruct.NguoiDung: SQL.Struct.NguoiDung nd = new SQL.Struct.NguoiDung(); nd.SoCMND = string.IsNullOrWhiteSpace(textBox2_SoCMND.Text) ? null : textBox2_SoCMND.Text; nd.TenDangNhap = string.IsNullOrWhiteSpace(textBox3_TenDangNhap.Text) ? null : textBox3_TenDangNhap.Text; nd.MatKhau = null; nd.HoTen = string.IsNullOrWhiteSpace(textBox4_HoTen.Text) ? null : textBox4_HoTen.Text; nd.SoDienThoai = string.IsNullOrWhiteSpace(textBox5_SoDienThoai.Text) ? null : textBox5_SoDienThoai.Text; nd.DiaChiThuongTru = string.IsNullOrWhiteSpace(textBox6_DiaChi.Text) ? null : textBox6_DiaChi.Text; nd.GioiTinh = radioButton1.Checked ? radioButton1.Checked : radioButton2.Checked; nd.NgaySinh = this.dateTimePicker1_NgaySinh.Value; nd.TenNguoiBaoHo = string.IsNullOrWhiteSpace(textBox7_TenNBH.Text) ? null : textBox7_TenNBH.Text; nd.SDTNguoiBaoHo = string.IsNullOrWhiteSpace(textBox8_SDT_NBH.Text) ? null : textBox8_SDT_NBH.Text; nd.DiaChiNguoiBaoHo = string.IsNullOrWhiteSpace(textBox9_DiaChiNBH.Text) ? null : textBox9_DiaChiNBH.Text; nd.DuongDanAnhDaiDien = null; return(nd); case EnumMyStruct.NhanVien: SQL.Struct.NhanVien nv = new SQL.Struct.NhanVien(); nv.MaNhanVien = string.IsNullOrWhiteSpace(textBox3_TenDangNhap.Text) ? null : textBox3_TenDangNhap.Text; nv.MatKhau = null; nv.TenNhanVien = string.IsNullOrWhiteSpace(textBox4_HoTen.Text) ? null : textBox4_HoTen.Text; nv.SoCMND = string.IsNullOrWhiteSpace(textBox2_SoCMND.Text) ? null : textBox2_SoCMND.Text; nv.DiaChi = string.IsNullOrWhiteSpace(textBox6_DiaChi.Text) ? null : textBox6_DiaChi.Text; nv.SoDienThoai = string.IsNullOrWhiteSpace(textBox5_SoDienThoai.Text) ? null : textBox5_SoDienThoai.Text; nv.GioiTinh = radioButton1.Checked ? radioButton1.Checked : radioButton2.Checked; nv.NgaySinh = this.dateTimePicker1_NgaySinh.Value; nv.LaQuanTriVien = this.checkBox1.Checked; return(nv); default: return(null); } }
public static bool NhanVien_AddNewRecord(SQL.Struct.NhanVien _value) { try { string t1 = "insert into NhanVien (MaNhanVien"; string t2 = "(N'" + _value.MaNhanVien + "'"; t1 += ", MatKhau"; t2 += ", N'" + _value.MaNhanVien + "'"; if (_value.TenNhanVien != null) { t1 += ", TenNhanVien"; t2 += ", N'" + _value.TenNhanVien + "'"; } if (_value.SoCMND != null) { t1 += ", SoCMND"; t2 += ", N'" + _value.SoCMND + "'"; } if (_value.DiaChi != null) { t1 += ", DiaChi"; t2 += ", N'" + _value.DiaChi + "'"; } if (_value.SoDienThoai != null) { t1 += ", SoDienThoai"; t2 += ", N'" + _value.SoDienThoai + "'"; } t1 += ", GioiTinh"; t2 += ", N'" + (_value.GioiTinh ? "True" : "False") + "'"; t1 += ", NgaySinh"; t2 += ", N'" + _value.NgaySinh.ToShortDateString() + "'"; t1 += ", LaQuanTriVien"; t2 += ", N'" + (_value.LaQuanTriVien ? "True" : "False") + "'"; if (_value.DuongDanAnhDaiDien != null) { t1 += ", DuongDanAnhDaiDien"; t2 += ", N'" + _value.DuongDanAnhDaiDien + "'"; } t1 += ") values " + t2 + ")"; commandSQL.CommandText = t1; commandSQL.Connection = connSQL; connSQL.Open(); commandSQL.ExecuteNonQuery(); connSQL.Close(); return(true); } catch { connSQL.Close(); return(false); } }
private void button3_AddNew_Click(object sender, EventArgs e) { if (!bSaveNewRecord) { // tạo một bản ghi mới switch (eTypeManage) { case EnumMyStruct.NguoiDung: this.textBox3_TenDangNhap.Enabled = true; EmptyInput(ref lTextbox); break; case EnumMyStruct.NhanVien: EmptyInput(ref lTextbox); this.textBox3_TenDangNhap.Text = SQL.ListData.GetDataFromSQL.NhanVien_GetNext_MaNhanVien(); break; default: break; } bSaveNewRecord = true; EditMode(true); this.button3_AddNew.Text = "Lưu bản ghi mới"; this.button4_Edit.Enabled = false; this.button5_SaveChange.Enabled = false; this.button6_Delete.Enabled = false; this.dataGridView1.Enabled = false; } else { // lưu bản ghi mới switch (eTypeManage) { case EnumMyStruct.NguoiDung: SQL.Struct.NguoiDung nd = AddDataToObject(EnumMyStruct.NguoiDung) as SQL.Struct.NguoiDung; if (SQL.ListData.GetDataFromSQL.NguoiDung_AddNewRecord(nd)) { MessageBox.Show("Tạo bản ghi mới thành công!"); } else { MessageBox.Show("Tạo bản ghi thất bại!"); } this.textBox3_TenDangNhap.Enabled = false; break; case EnumMyStruct.NhanVien: SQL.Struct.NhanVien nv = AddDataToObject(EnumMyStruct.NhanVien) as SQL.Struct.NhanVien; if (SQL.ListData.GetDataFromSQL.NhanVien_AddNewRecord(nv)) { MessageBox.Show("Tạo bản ghi mới thành công!"); } else { MessageBox.Show("Tạo bản ghi thất bại!"); } break; default: break; } bSaveNewRecord = false; this.button3_AddNew.Text = "Thêm mới"; this.button8_Cancel_Click(sender, e); this.dataGridView1.Enabled = true; } }