private void FormEditBarang_Load(object sender, EventArgs e) { try { using (var dao = new BarangDAO()) { Barang barang = dao.GetDataBarangByKode(_kode); if (barang != null) { this.txtkode.Text = barang.Kode; this.txtnama.Text = barang.Nama; this.txtjumlah.Text = Convert.ToInt32(barang.Quantity).ToString(); this.txtharga.Text = Convert.ToDecimal(barang.Harga).ToString(); this.txtpajak.Text = barang.Pajak; } } } catch (Exception ex) { MessageBox.Show("Edit Form!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } }
public bool TambahData(Barang barang) { bool result = false; try { if (Convert.ToDecimal(barang.Harga) > 0 && Convert.ToInt32(barang.Pajak) >= 0 && barang.Quantity > 0) { listBarang.Add(barang); result = true; } else { throw new ArgumentException(); } } catch (Exception ex) { throw new ArgumentException(); } return(result); }
private void btnsimpan_Click(object sender, EventArgs e) { if (this.txtnama.Text.Trim() == "") { MessageBox.Show("Sorry, isi nama barang terlebih dahulu ...", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); this.txtnama.Focus(); } else if (this.txtharga.Text.Trim() == "") { MessageBox.Show("Sorry, isi harga terlebih dahulu ...", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); this.txtharga.Focus(); } else if (this.txtpajak.Text.Trim() == "") { MessageBox.Show("Sorry, isi pajak terlebih dahulu ...", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); this.txtpajak.Focus(); } else { try { Barang barang = new Barang { Kode = this.txtkode.Text.Trim(), Nama = this.txtnama.Text.Trim(), Harga = Convert.ToDecimal(this.txtharga.Text.Trim()), Pajak = this.txtpajak.Text.Trim() }; _result = new BarangDAO().Update(barang) > 0; this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
public Barang GetDataBarangByKode(string kode) { Barang barang = null; try { string sqlString = @"select * from barang where kode = @kode"; using (SqlCommand cmd = new SqlCommand()) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@kode", kode); using (SqlDataReader reader = cmd.ExecuteReader()) { // if (reader.HasRows) // { if (reader.Read()) { barang = new Barang { Kode = reader["No"].ToString(), Nama = reader["nama"].ToString(), Quantity = Convert.ToInt32(reader["Quantity"].ToString()), Harga = Convert.ToDecimal(reader["harga"].ToString()), Pajak = reader["pajak"].ToString(), }; // } } } } } catch (Exception ex) { throw ex; } return(barang); }
public bool UpdateData(Barang lama, Barang baru) { bool result = false; try { if (Convert.ToDecimal(baru.Harga) > 0 && Convert.ToInt32(baru.Pajak) >= 0 && baru.Quantity > 0) { if (ItemIsExist(lama)) { for (int i = 0; i < listBarang.Count; i++) { Barang item = listBarang[i]; if (item.Nama.ToLower().Trim().Equals(lama.Nama.ToLower().Trim())) { listBarang[i] = baru; result = true; break; } } } else { throw new ArgumentException(); } } else { throw new ArgumentException(); } } catch (Exception ex) { throw new ArgumentException(); } return(result); }