public Barang GetDataBarangByKode(string kode) { Barang result = null; try { using (SqlCommand cmd = new SqlCommand(@"select * from barang where kode = @kode", _conn)) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@Kode", kode); using (SqlDataReader reader = cmd.ExecuteReader()) { if (reader.HasRows) { if (reader.Read()) { result = new Barang { Kode = reader["Kode"].ToString(), Nama = reader["Nama"].ToString(), Harga = Convert.ToDecimal(reader["Harga"]), Jumlah = int.Parse(reader["Jumlah"].ToString()), Pajak = int.Parse(reader["Pajak"].ToString()) }; } } } } } catch (Exception ex) { throw ex; } return(result); }
public void UpdateBarang(Barang barang) { try { _trans = _conn.BeginTransaction(); using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = _conn; cmd.Transaction = _trans; cmd.CommandText = @"update barang set nama = @nama, jumlah = @jumlah , harga = @harga, pajak = @pajak where kode = @kode"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@kode", barang.Kode); cmd.Parameters.AddWithValue("@nama", barang.Nama); cmd.Parameters.AddWithValue("@jumlah", barang.Jumlah); cmd.Parameters.AddWithValue("@harga", barang.Harga); cmd.Parameters.AddWithValue("@pajak", barang.Pajak); cmd.ExecuteNonQuery(); } _trans.Commit(); } catch (Exception ex) { if (_trans != null) { _trans.Rollback(); } throw ex; } finally { if (_trans != null) { _trans.Dispose(); } } }
// method untuk delete barang public void DeleteBarang(Barang barang) { Barang dataToDelete = null; if (CheckItemExist(barang)) { for (int i = 0; i < listBrg.Count; i++) { dataToDelete = listBrg[i]; if (dataToDelete.Kode.Equals(barang.Kode)) { break; } } if (dataToDelete != null) { listBrg.Remove(dataToDelete); } } else { throw new ArgumentException("Data tersebut tidak ada"); } }