0% found this document useful (0 votes)
4 views10 pages

File FrmInputDataBarang

The document contains the code for a Windows Forms application that manages product data input in a store application. It includes functionalities for loading product categories, validating input, saving new products or updating existing ones, and handling user interactions through buttons and forms. The code also features error handling for database operations and input validation to ensure data integrity.

Uploaded by

mrevo88
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

File FrmInputDataBarang

The document contains the code for a Windows Forms application that manages product data input in a store application. It includes functionalities for loading product categories, validating input, saving new products or updating existing ones, and handling user interactions through buttons and forms. The code also features error handling for database operations and input validation to ensure data integrity.

Uploaded by

mrevo88
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

File FrmInputDataBarang.

cs
using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using TokoJoniApp.DAL;

namespace TokoJoniApp.Forms
{
public partial class FrmInputDataBarang : Form
{
private bool isEditMode = false;
private string originalKodeBarang = "";
private DataTable dtKelompokBarang; // Menyimpan data kelompok barang

public string KodeBarangBaru => kode_barang_TxBox_inBarang.Text.Trim();


public string NamaBarangBaru => nama_barang_TxBox_inBarang.Text.Trim();

public FrmInputDataBarang()
{
InitializeComponent();
LoadKelompokBarang(); // Muat data kelompok barang
}

private void LoadKelompokBarang()


{
try
{
using (var conn = new Database().GetConnection())
{
string query = "SELECT KelompokBarang_ID, NamaKelompok FROM
tbKelompokBarang";
using (var cmd = new OleDbCommand(query, conn))
{
conn.Open();
dtKelompokBarang = new DataTable();
dtKelompokBarang.Load(cmd.ExecuteReader());

// Set ComboBox binding


kelompok_ComBox_inBarang.DataSource = dtKelompokBarang;
kelompok_ComBox_inBarang.DisplayMember = "NamaKelompok"; //
Tampilan untuk user
kelompok_ComBox_inBarang.ValueMember = "KelompokBarang_ID";
// Nilai yang digunakan saat menyimpan
}
}
}
catch (Exception ex)
{
MessageBox.Show("Gagal memuat kelompok barang: " + ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void simpan_btn_inBarang_Click(object sender, EventArgs e)


{
string kodeBarang = kode_barang_TxBox_inBarang.Text.Trim();
string namaBarang = nama_barang_TxBox_inBarang.Text.Trim();
object kelompokID = kelompok_ComBox_inBarang.SelectedValue; // Ambil
KelompokBarang_ID sebagai objek
decimal hargaBeli = 0;
decimal hargaJual = 0;
decimal? hargaKhusus = null;

// Validasi input wajib


if (string.IsNullOrEmpty(kodeBarang) ||
string.IsNullOrEmpty(namaBarang) || kelompokID == null)
{
MessageBox.Show("Semua field wajib diisi!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

// Validasi input numerik


if (!decimal.TryParse(harga_beli_TxBox_inBarang.Text.Trim(), out
hargaBeli) ||
!decimal.TryParse(harga_jual_TxBox_inBarang.Text.Trim(), out
hargaJual))
{
MessageBox.Show("Harga beli dan jual harus berupa angka!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

// Validasi harga khusus


decimal hk = 0; // Inisialisasi awal
bool hasHargaKhusus = !
string.IsNullOrEmpty(harga_khusus_TxBox_inBarang.Text.Trim());

if (hasHargaKhusus && !
decimal.TryParse(harga_khusus_TxBox_inBarang.Text.Trim(), out hk))
{
MessageBox.Show("Harga khusus harus berupa angka!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

// Jika harga khusus tidak kosong


if (hasHargaKhusus)
{
hargaKhusus = hk;
}

// Cek apakah harga beli <= 50


if (hargaBeli <= 50)
{
DialogResult result = MessageBox.Show("Harga beli <= Rp50. Apakah
Anda yakin?", "Konfirmasi", MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
harga_beli_TxBox_inBarang.Focus();
return;
}
}

try
{
using (var conn = new Database().GetConnection())
{
conn.Open();

// Cek apakah kode barang sudah ada


string checkQuery = "SELECT COUNT(*) FROM tbBarang WHERE
kode_barang = ?";
using (var checkCmd = new OleDbCommand(checkQuery, conn))
{
checkCmd.Parameters.AddWithValue("?", kodeBarang);
int count = Convert.ToInt32(checkCmd.ExecuteScalar());

if (count > 0 && !isEditMode)


{
// Konfirmasi update jika kode barang sudah ada
DialogResult result = MessageBox.Show("Kode barang
sudah ada. Apakah ingin mengupdate data?", "Konfirmasi", MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
return;
}
}
}

// Siapkan query
string query = "";
OleDbCommand cmd;

if (isEditMode)
{
// Update jika sudah ada
query = "UPDATE tbBarang SET nama_barang = ?,
KelompokBarang_ID = ?, harga_beli = ?, harga_jual = ?, harga_khusus = ? WHERE
kode_barang = ?";
}
else
{
// Insert barang baru
query = "INSERT INTO tbBarang (kode_barang, nama_barang,
KelompokBarang_ID, harga_beli, harga_jual, harga_khusus) VALUES
(?, ?, ?, ?, ?, ?)";
}

using (cmd = new OleDbCommand(query, conn))


{
cmd.Parameters.AddWithValue("?", namaBarang);
cmd.Parameters.AddWithValue("?", kelompokID); // Gunakan
KelompokBarang_ID (INTEGER)
cmd.Parameters.AddWithValue("?", hargaBeli);
cmd.Parameters.AddWithValue("?", hargaJual);

// Tambahkan harga khusus (nullable)


if (hasHargaKhusus)
cmd.Parameters.AddWithValue("?", hk);
else
cmd.Parameters.AddWithValue("?", DBNull.Value);

// Jika mode edit, tambahkan WHERE clause


if (isEditMode)
cmd.Parameters.AddWithValue("?", kodeBarang);
cmd.ExecuteNonQuery();
string successMessage = isEditMode ? "Data berhasil
diperbarui!" : "Data berhasil disimpan!";
MessageBox.Show(successMessage, "Info",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}

// Tanyakan apakah ingin tambah data lagi


DialogResult saveResult = MessageBox.Show("Data telah
tersimpan. Apakah mau tambah data lagi?", "Info", MessageBoxButtons.YesNo);
if (saveResult == DialogResult.No)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
ClearForm();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Gagal menyimpan data: " + ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

public void LoadBarangData(string kodeBarang)


{
try
{
using (var conn = new Database().GetConnection())
{
string query = "SELECT * FROM tbBarang WHERE kode_barang = ?";
using (var cmd = new OleDbCommand(query, conn))
{
cmd.Parameters.AddWithValue("?", kodeBarang);
conn.Open();
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
isEditMode = true;
originalKodeBarang = kodeBarang;
kode_barang_TxBox_inBarang.Text = kodeBarang;
kode_barang_TxBox_inBarang.ReadOnly = true;
nama_barang_TxBox_inBarang.Text =
reader["nama_barang"].ToString();

// Set ComboBox berdasarkan KelompokBarang_ID


if
(int.TryParse(reader["KelompokBarang_ID"].ToString(), out int kelompokID))
{
kelompok_ComBox_inBarang.SelectedValue =
kelompokID;
}
harga_beli_TxBox_inBarang.Text =
reader["harga_beli"].ToString();
harga_jual_TxBox_inBarang.Text =
reader["harga_jual"].ToString();
harga_khusus_TxBox_inBarang.Text =
reader["harga_khusus"]?.ToString() ?? "";
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Gagal memuat data barang: " + ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void ClearForm()


{
kode_barang_TxBox_inBarang.Clear();
nama_barang_TxBox_inBarang.Clear();
kelompok_ComBox_inBarang.SelectedIndex = -1;
harga_beli_TxBox_inBarang.Clear();
harga_jual_TxBox_inBarang.Clear();
harga_khusus_TxBox_inBarang.Clear();
isEditMode = false;
kode_barang_TxBox_inBarang.ReadOnly = false;
}

private void close_btn_inBarang_Click(object sender, EventArgs e)


{
this.Close();
}

private void FrmInputDataBarang_Load(object sender, EventArgs e)


{
kode_barang_TxBox_inBarang.Focus();
}

private void ValidateHargaBeliOnLeave(object sender, EventArgs e)


{
if (decimal.TryParse(harga_beli_TxBox_inBarang.Text, out decimal
hargaBeli))
{
if (hargaBeli <= 50)
{
MessageBox.Show("Harga beli harus lebih dari Rp50!",
"Peringatan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
harga_beli_TxBox_inBarang.Focus();
}
}
}
}
}

File FrmInputDataBarang.Designer.cs
using System;
using System.Windows.Forms;

namespace TokoJoniApp.Forms
{
partial class FrmInputDataBarang
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed;
otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.simpan_btn_inBarang = new System.Windows.Forms.Button();
this.clear_btn_inBarang = new System.Windows.Forms.Button();
this.close_btn_inBarang = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.kode_barang_TxBox_inBarang = new System.Windows.Forms.TextBox();
this.nama_barang_TxBox_inBarang = new System.Windows.Forms.TextBox();
this.harga_beli_TxBox_inBarang = new System.Windows.Forms.TextBox();
this.harga_jual_TxBox_inBarang = new System.Windows.Forms.TextBox();
this.harga_khusus_TxBox_inBarang = new System.Windows.Forms.TextBox();
this.kelompok_ComBox_inBarang = new System.Windows.Forms.ComboBox();
this.user_ID_error_Label_inUser = new System.Windows.Forms.Label(); //
Tambahkan jika dibutuhkan untuk validasi
this.SuspendLayout();

//
// simpan_btn_inBarang
//
this.simpan_btn_inBarang.Location = new System.Drawing.Point(50, 230);
this.simpan_btn_inBarang.Name = "simpan_btn_inBarang";
this.simpan_btn_inBarang.Size = new System.Drawing.Size(100, 30);
this.simpan_btn_inBarang.TabIndex = 7;
this.simpan_btn_inBarang.Text = "Simpan";
this.simpan_btn_inBarang.UseVisualStyleBackColor = true;
this.simpan_btn_inBarang.Click += new
System.EventHandler(this.simpan_btn_inBarang_Click);

//
// clear_btn_inBarang
//
this.clear_btn_inBarang.Location = new System.Drawing.Point(160, 230);
this.clear_btn_inBarang.Name = "clear_btn_inBarang";
this.clear_btn_inBarang.Size = new System.Drawing.Size(100, 30);
this.clear_btn_inBarang.TabIndex = 8;
this.clear_btn_inBarang.Text = "Clear";
this.clear_btn_inBarang.UseVisualStyleBackColor = true;
this.clear_btn_inBarang.Click += new
System.EventHandler(this.clear_btn_inBarang_Click);

//
// close_btn_inBarang
//
this.close_btn_inBarang.Location = new System.Drawing.Point(270, 230);
this.close_btn_inBarang.Name = "close_btn_inBarang";
this.close_btn_inBarang.Size = new System.Drawing.Size(100, 30);
this.close_btn_inBarang.TabIndex = 9;
this.close_btn_inBarang.Text = "Tutup";
this.close_btn_inBarang.UseVisualStyleBackColor = true;
this.close_btn_inBarang.Click += new
System.EventHandler(this.close_btn_inBarang_Click);

//
// label1 (Kode Barang)
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(50, 30);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(70, 20);
this.label1.Text = "Kode Barang";

//
// label2 (Nama Barang)
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(50, 60);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(70, 20);
this.label2.Text = "Nama Barang";

//
// label3 (Kelompok)
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(50, 90);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(70, 20);
this.label3.Text = "Kelompok";

//
// label4 (Harga Beli)
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(50, 120);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(70, 20);
this.label4.Text = "Harga Beli";

//
// label5 (Harga Jual)
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(50, 150);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(70, 20);
this.label5.Text = "Harga Jual";

//
// label6 (Harga Khusus)
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(50, 180);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(70, 20);
this.label6.Text = "Harga Khusus";

//
// kode_barang_TxBox_inBarang
//
this.kode_barang_TxBox_inBarang.Location = new
System.Drawing.Point(150, 27);
this.kode_barang_TxBox_inBarang.Name = "kode_barang_TxBox_inBarang";
this.kode_barang_TxBox_inBarang.Size = new System.Drawing.Size(150,
25);
this.kode_barang_TxBox_inBarang.TabIndex = 0;

//
// nama_barang_TxBox_inBarang
//
this.nama_barang_TxBox_inBarang.Location = new
System.Drawing.Point(150, 57);
this.nama_barang_TxBox_inBarang.Name = "nama_barang_TxBox_inBarang";
this.nama_barang_TxBox_inBarang.Size = new System.Drawing.Size(200,
25);
this.nama_barang_TxBox_inBarang.TabIndex = 1;

//
// harga_beli_TxBox_inBarang
//
this.harga_beli_TxBox_inBarang.Location = new System.Drawing.Point(150,
117);
this.harga_beli_TxBox_inBarang.Name = "harga_beli_TxBox_inBarang";
this.harga_beli_TxBox_inBarang.Size = new System.Drawing.Size(100, 25);
this.harga_beli_TxBox_inBarang.TabIndex = 3;
this.harga_beli_TxBox_inBarang.Leave += new
System.EventHandler(this.ValidateHargaBeliOnLeave);

//
// harga_jual_TxBox_inBarang
//
this.harga_jual_TxBox_inBarang.Location = new System.Drawing.Point(150,
147);
this.harga_jual_TxBox_inBarang.Name = "harga_jual_TxBox_inBarang";
this.harga_jual_TxBox_inBarang.Size = new System.Drawing.Size(100, 25);
this.harga_jual_TxBox_inBarang.TabIndex = 4;
this.harga_jual_TxBox_inBarang.Leave += new
System.EventHandler(this.ValidateHargaJualOnLeave);

//
// harga_khusus_TxBox_inBarang
//
this.harga_khusus_TxBox_inBarang.Location = new
System.Drawing.Point(150, 177);
this.harga_khusus_TxBox_inBarang.Name = "harga_khusus_TxBox_inBarang";
this.harga_khusus_TxBox_inBarang.Size = new System.Drawing.Size(100,
25);
this.harga_khusus_TxBox_inBarang.TabIndex = 5;
this.harga_khusus_TxBox_inBarang.Leave += new
System.EventHandler(this.ValidateHargaKhususOnLeave);

//
// kelompok_ComBox_inBarang
//
this.kelompok_ComBox_inBarang.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList;
this.kelompok_ComBox_inBarang.FormattingEnabled = true;
this.kelompok_ComBox_inBarang.Location = new System.Drawing.Point(150,
87);
this.kelompok_ComBox_inBarang.Name = "kelompok_ComBox_inBarang";
this.kelompok_ComBox_inBarang.Size = new System.Drawing.Size(150, 25);
this.kelompok_ComBox_inBarang.TabIndex = 2;

//
// user_ID_error_Label_inUser (untuk validasi error)
//
this.user_ID_error_Label_inUser.AutoSize = true;
this.user_ID_error_Label_inUser.ForeColor = System.Drawing.Color.Red;
this.user_ID_error_Label_inUser.Location = new
System.Drawing.Point(150, 50);
this.user_ID_error_Label_inUser.Name = "user_ID_error_Label_inUser";
this.user_ID_error_Label_inUser.Size = new System.Drawing.Size(0, 13);
this.user_ID_error_Label_inUser.TabIndex = 10;

//
// FrmInputDataBarang
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(400, 300);
this.Controls.Add(this.kelompok_ComBox_inBarang);
this.Controls.Add(this.harga_khusus_TxBox_inBarang);
this.Controls.Add(this.harga_jual_TxBox_inBarang);
this.Controls.Add(this.harga_beli_TxBox_inBarang);
this.Controls.Add(this.nama_barang_TxBox_inBarang);
this.Controls.Add(this.kode_barang_TxBox_inBarang);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.close_btn_inBarang);
this.Controls.Add(this.clear_btn_inBarang);
this.Controls.Add(this.simpan_btn_inBarang);
this.Name = "FrmInputDataBarang";
this.Text = "Input Data Barang";
this.ResumeLayout(false);
this.PerformLayout();
}

#endregion

private System.Windows.Forms.Button simpan_btn_inBarang;


private System.Windows.Forms.Button clear_btn_inBarang;
private System.Windows.Forms.Button close_btn_inBarang;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox kode_barang_TxBox_inBarang;
private System.Windows.Forms.TextBox nama_barang_TxBox_inBarang;
private System.Windows.Forms.TextBox harga_beli_TxBox_inBarang;
private System.Windows.Forms.TextBox harga_jual_TxBox_inBarang;
private System.Windows.Forms.TextBox harga_khusus_TxBox_inBarang;
private System.Windows.Forms.ComboBox kelompok_ComBox_inBarang;
private System.Windows.Forms.Label user_ID_error_Label_inUser;
}
}

You might also like