public Car_ManufacturerDTO FindByName(string Name) { Car_ManufacturerDTO result = null; string SQL = "SELECT ID, Name FROM Car_Manufacturers WHERE Name = @Name"; SqlConnection cnn = DBUtils.GetConnection(); SqlCommand cmd = new SqlCommand(SQL, cnn); cmd.Parameters.AddWithValue("@Name", Name); try { if (cnn.State == ConnectionState.Closed) { cnn.Open(); SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (rd.Read()) { result = new Car_ManufacturerDTO { ID = rd.GetInt32(0), Name = rd.GetString(1) }; } } } catch (SqlException ex) { throw new Exception(ex.Message); } return(result); }
public bool UpdateManufacturer(Car_ManufacturerDTO dto) { bool result = false; string SQL = "UPDATE Car_Manufacturers SET Name = @Name WHERE ID = @ID"; SqlConnection cnn = DBUtils.GetConnection(); SqlCommand cmd = new SqlCommand(SQL, cnn); cmd.Parameters.AddWithValue("@Name", dto.Name); cmd.Parameters.AddWithValue("@ID", dto.ID); try { if (cnn.State == ConnectionState.Closed) { cnn.Open(); } result = cmd.ExecuteNonQuery() > 0; } catch (Exception ex) { throw new Exception(ex.Message); } finally { if (cnn.State == ConnectionState.Open) { cnn.Close(); } } return(result); }
public List <Car_ManufacturerDTO> GetListManufacturer() { List <Car_ManufacturerDTO> result = null; string SQL = "SELECT ID, Name FROM Car_Manufacturers"; SqlConnection cnn = DBUtils.GetConnection(); SqlCommand cmd = new SqlCommand(SQL, cnn); try { if (cnn.State == ConnectionState.Closed) { cnn.Open(); SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection); while (rd.Read()) { if (result == null) { result = new List <Car_ManufacturerDTO>(); } Car_ManufacturerDTO dto = new Car_ManufacturerDTO { ID = rd.GetInt32(0), Name = rd.GetString(1) }; result.Add(dto); } } } catch (SqlException ex) { throw new Exception(ex.Message); } return(result); }
private void btnEdit_Click(object sender, EventArgs e) { string sID = dgvManufacturers.CurrentRow.Cells[0].Value.ToString(); string Name = dgvManufacturers.CurrentRow.Cells[1].Value.ToString(); int ID = -1; try { ID = int.Parse(sID); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); return; } Car_ManufacturerDTO dto = new Car_ManufacturerDTO { ID = ID, Name = Name }; frmEditManufacturer frm = new frmEditManufacturer(dto); DialogResult result = frm.ShowDialog(); if (result == DialogResult.OK) { loadData(); } }
private void btnSave_Click(object sender, EventArgs e) { string Manufacturer_Name = txtName.Text.Trim(); if (string.IsNullOrEmpty(Manufacturer_Name)) { MessageBox.Show("Please input Name!"); return; } Car_ManufacturerDAO dao = new Car_ManufacturerDAO(); Car_ManufacturerDTO dto = dao.FindByName(Manufacturer_Name); if (dto != null) { MessageBox.Show("Manufacturer is exist!"); return; } else { bool result = dao.AddNewManufacturer(Manufacturer_Name); if (result) { MessageBox.Show("Add new Manufacturer successfully!!"); } else { MessageBox.Show("Add new Manufacturer failed!!"); } this.DialogResult = DialogResult.OK; } }
private void btnDelete_Click(object sender, EventArgs e) { string sID = dgvManufacturers.CurrentRow.Cells[0].Value.ToString(); string Name = dgvManufacturers.CurrentRow.Cells[1].Value.ToString(); DialogResult confirmResult = MessageBox.Show("Do you want to delete Manufacturer: " + Name + "???", "Confirm Exit!!", MessageBoxButtons.YesNo); if (confirmResult == DialogResult.Yes) { int ID = -1; try { ID = int.Parse(sID); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); return; } Car_ManufacturerDTO dto = new Car_ManufacturerDTO { ID = ID, Name = Name }; bool result = dao.DeleteManufacturer(dto); if (result) { MessageBox.Show("Delete Manufacturer successfully!!"); loadData(); } else { MessageBox.Show("Delete Manufacturer failed!!"); } } }
public frmEditManufacturer(Car_ManufacturerDTO dto) : this() { this.dto = dto; }