/// <summary> /// update row in the table /// </summary> /// <param name="businessObject">business object</param> /// <returns>true for successfully updated</returns> public bool Update(BENHVIEN businessObject) { SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandText = "dbo.[BENHVIEN_Update]"; sqlCommand.CommandType = CommandType.StoredProcedure; // Use connection object of base class sqlCommand.Connection = MainConnection; try { sqlCommand.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.ID)); sqlCommand.Parameters.Add(new SqlParameter("@TEN", SqlDbType.NVarChar, 2147483647, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.TEN)); sqlCommand.Parameters.Add(new SqlParameter("@DIACHI", SqlDbType.NVarChar, 2147483647, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.DIACHI)); sqlCommand.Parameters.Add(new SqlParameter("@KIHIEU", SqlDbType.NVarChar, 2147483647, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.KIHIEU)); sqlCommand.Parameters.Add(new SqlParameter("@GHICHU", SqlDbType.NVarChar, 2147483647, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, businessObject.GHICHU)); MainConnection.Open(); sqlCommand.ExecuteNonQuery(); return(true); } catch //(Exception ex) { return(false); //throw new Exception("BENHVIEN::Update::Error occured.", ex); } finally { MainConnection.Close(); sqlCommand.Dispose(); } }
private BENHVIEN getBENHVIENByForm() { BENHVIEN ans = new BENHVIEN(); ans.TEN = txtTen.Text; ans.KIHIEU = txtKiHieu.Text; ans.DIACHI = txtDiaChi.Text; return(ans); }
/// <summary> /// Populate business objects from the data reader /// </summary> /// <param name="dataReader">data reader</param> /// <returns>list of BENHVIEN</returns> internal List <BENHVIEN> PopulateObjectsFromReader(IDataReader dataReader) { List <BENHVIEN> list = new List <BENHVIEN>(); while (dataReader.Read()) { BENHVIEN businessObject = new BENHVIEN(); PopulateBusinessObjectFromReader(businessObject, dataReader); list.Add(businessObject); } return(list); }
private void btnSua_Click(object sender, EventArgs e) { if (!CheckLuaChon()) { return; } if (btnSua.Text == "Sửa") { btnSua.Text = "Lưu"; btnXoa.Text = "Hủy"; btnThem.Enabled = false; UnlockControl(); return; } if (btnSua.Text == "Lưu") { if (Check()) { btnSua.Text = "Sửa"; btnXoa.Text = "Xóa"; LockControl(); BENHVIEN cu = getBENHVIENByID(); BENHVIEN moi = getBENHVIENByForm(); CapNhat(ref cu, moi); if (BENHVIENService.Update(cu)) { MessageBox.Show("Sửa thông tin bệnh viện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Sửa thông tin bệnh viện thất bại\n", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } LoadDgvBENHVIEN(); } return; } }
private void btnXoa_Click(object sender, EventArgs e) { if (btnXoa.Text == "Xóa") { if (!CheckLuaChon()) { return; } BENHVIEN cu = getBENHVIENByID(); DialogResult rs = MessageBox.Show("Bạn có chắc chắn xóa bệnh viện " + cu.TEN + "?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (rs == DialogResult.Cancel) { return; } try { BENHVIENService.Delete(new BENHVIENKeys(cu.ID)); MessageBox.Show("Xóa thông tin bệnh viện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show("Xóa thông tin bệnh viện thất bại\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } LoadDgvBENHVIEN(); return; } if (btnXoa.Text == "Hủy") { btnSua.Text = "Sửa"; btnThem.Text = "Thêm"; btnXoa.Text = "Xóa"; LockControl(); UpdateDetail(); return; } }
private bool CheckLuaChon() { BENHVIEN tg = getBENHVIENByID(); if (tg.ID == 0) { MessageBox.Show("Chưa có bệnh viện nào được chọn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } return(true); }
private BENHVIEN getBENHVIENByID() { try { int id = (int)dgvBENHVIEN.GetFocusedRowCellValue("ID"); BENHVIEN ans = BENHVIENService.GetAllBy(BENHVIEN.BENHVIENFields.ID, id).FirstOrDefault(); if (ans == null) { return(new BENHVIEN()); } return(ans); } catch { return(new BENHVIEN()); } }
private void btnThem_Click(object sender, EventArgs e) { if (btnThem.Text == "Thêm") { btnSua.Enabled = false; btnThem.Text = "Lưu"; btnXoa.Text = "Hủy"; ClearControl(); UnlockControl(); return; } if (btnThem.Text == "Lưu") { if (Check()) { btnThem.Text = "Thêm"; btnXoa.Text = "Xóa"; LockControl(); BENHVIEN moi = getBENHVIENByForm(); if (BENHVIENService.Insert(moi)) { MessageBox.Show("Thêm thông tin bệnh viện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Thêm thông tin bệnh viện thất bại\n", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error); } LoadDgvBENHVIEN(); } return; } }
private void UpdateDetail() { try { BENHVIEN tg = getBENHVIENByID(); if (tg.ID == 0) { return; } txtTen.Text = tg.TEN; txtKiHieu.Text = tg.KIHIEU; txtDiaChi.Text = tg.DIACHI; } catch { } }
/// <summary> /// Select by primary key /// </summary> /// <param name="keys">primary keys</param> /// <returns>BENHVIEN business object</returns> public BENHVIEN SelectByPrimaryKey(BENHVIENKeys keys) { SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandText = "dbo.[BENHVIEN_SelectByPrimaryKey]"; sqlCommand.CommandType = CommandType.StoredProcedure; // Use connection object of base class sqlCommand.Connection = MainConnection; try { sqlCommand.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, keys.ID)); MainConnection.Open(); IDataReader dataReader = sqlCommand.ExecuteReader(); if (dataReader.Read()) { BENHVIEN businessObject = new BENHVIEN(); PopulateBusinessObjectFromReader(businessObject, dataReader); return(businessObject); } else { return(null); } } catch //(Exception ex) { return(null); //throw new Exception("BENHVIEN::SelectByPrimaryKey::Error occured.", ex); } finally { MainConnection.Close(); sqlCommand.Dispose(); } }
/// <summary> /// Populate business object from data reader /// </summary> /// <param name="businessObject">business object</param> /// <param name="dataReader">data reader</param> internal void PopulateBusinessObjectFromReader(BENHVIEN businessObject, IDataReader dataReader) { businessObject.ID = dataReader.GetInt32(dataReader.GetOrdinal(BENHVIEN.BENHVIENFields.ID.ToString())); if (!dataReader.IsDBNull(dataReader.GetOrdinal(BENHVIEN.BENHVIENFields.TEN.ToString()))) { businessObject.TEN = dataReader.GetString(dataReader.GetOrdinal(BENHVIEN.BENHVIENFields.TEN.ToString())); } if (!dataReader.IsDBNull(dataReader.GetOrdinal(BENHVIEN.BENHVIENFields.DIACHI.ToString()))) { businessObject.DIACHI = dataReader.GetString(dataReader.GetOrdinal(BENHVIEN.BENHVIENFields.DIACHI.ToString())); } if (!dataReader.IsDBNull(dataReader.GetOrdinal(BENHVIEN.BENHVIENFields.KIHIEU.ToString()))) { businessObject.KIHIEU = dataReader.GetString(dataReader.GetOrdinal(BENHVIEN.BENHVIENFields.KIHIEU.ToString())); } if (!dataReader.IsDBNull(dataReader.GetOrdinal(BENHVIEN.BENHVIENFields.GHICHU.ToString()))) { businessObject.GHICHU = dataReader.GetString(dataReader.GetOrdinal(BENHVIEN.BENHVIENFields.GHICHU.ToString())); } }
private void CapNhat(ref BENHVIEN cu, BENHVIEN moi) { cu.TEN = moi.TEN; cu.KIHIEU = moi.KIHIEU; cu.DIACHI = moi.DIACHI; }