Barang

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Aplikasi_Penjualan_Boneka
{
public partial class Barang : Form
{
public Barang()
{
InitializeComponent();
}
MySqlConnection koneksi = new MySqlConnection("server=localhost;
database=db_winda; uid=root; password=;");
MySqlCommand perintah;

private void Barang_Load(object sender, EventArgs e)


{
string querySelect = "select * from barang";
DataTable table = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter(querySelect, koneksi);
adapter.Fill(table);
dataGridView1.DataSource = table;
// TODO: This line of code loads data into the 'dataSet1.barang' table.
You can move, or remove it, as needed.

private void dataGridView1_CellContentClick(object sender,


DataGridViewCellEventArgs e)
{

}
public void bukaKoneksi()
{
if (koneksi.State == ConnectionState.Closed)
{
koneksi.Open();
}
}
public void tutupKoneksi()
{
if (koneksi.State == ConnectionState.Open)
{
koneksi.Close();
}
}

public void eksekusiQuery(string query)


{
try
{
bukaKoneksi();
perintah = new MySqlCommand(query, koneksi);
if (perintah.ExecuteNonQuery() == 1)
{
MessageBox.Show("DATA, TERHUBUNG");
txtid.Text = "";
txtnama.Text = "";
comboBox1.Text = "";

}
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
finally
{
tutupKoneksi();
}
}

private void button4_Click(object sender, EventArgs e)


{
string queryInsert = "INSERT INTO
barang(idBarang,Namabarang,Jenisbarang) VALUES('" +
txtid.Text + "','" +
txtnama.Text + "','" +
comboBox1.Text + "')";
eksekusiQuery(queryInsert);
Barang_Load(this, null);
}

private void btnedit_Click(object sender, EventArgs e)


{
string queryUpdate = "UPDATE barang set Namabarang='" +
txtnama.Text + "', Jenisbarang='" + comboBox1.Text
+ "' where idBarang=" + int.Parse(txtid.Text);
eksekusiQuery(queryUpdate);
Barang_Load(this, null);
}

private void btndel_Click(object sender, EventArgs e)


{
string queryDelete = "DELETE FROM barang WHERE idBarang=" +
int.Parse(txtid.Text);
eksekusiQuery(queryDelete);
Barang_Load(this, null);
}

private void button5_Click(object sender, EventArgs e)


{
MySqlDataReader baca;
string select = "SELECT * FROM barang WHERE idBarang=" + txtid.Text;
perintah = new MySqlCommand(select, koneksi);
bukaKoneksi();
baca = perintah.ExecuteReader();
if (baca.Read())
{
txtnama.Text = baca.GetString("Namabarang");
comboBox1.Text = baca.GetString("Jenisbarang");

}
else
{
MessageBox.Show("Barang tidak ditemukan");
}
tutupKoneksi();
}

private void btnback_Click(object sender, EventArgs e)


{
this.Hide();
Menu m = new Menu();
m.ShowDialog();
}

private void panel1_Paint(object sender, PaintEventArgs e)


{

private void Barang_MouseClick(object sender, MouseEventArgs e)


{

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)


{
txtid.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
txtnama.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
comboBox1.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
}

private void label4_Click(object sender, EventArgs e)


{

}
}
}

You might also like