Using Using Using Using Namespace Static Class
Using Using Using Using Namespace Static Class
Using Using Using Using Namespace Static Class
cs
}
}
//panggil library2 C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Apotik //nama project/workspace aplikasi
{
public partial class FormMasterObat : Form // deklarasi class Formmasterobat sebagai form
{
static string conString = "server=localhost;database=apotik;uid=root;password=";
MySqlConnection con = new MySqlConnection(conString);
MySqlCommand cmd;
MySqlDataAdapter adapter;
DataTable dt = new DataTable();
public FormMasterObat()
{
InitializeComponent();
// untuk datagridview property
dataGridViewobat.ColumnCount = 9;
dataGridViewobat.Columns[0].HeaderText = "Kode Obat";
dataGridViewobat.Columns[1].HeaderText = "Nama Obat";
dataGridViewobat.Columns[2].HeaderText = "Kategori";
dataGridViewobat.Columns[3].HeaderText = "Golongan";
dataGridViewobat.Columns[4].HeaderText = "Kemasan";
dataGridViewobat.Columns[5].HeaderText = "Harga Beli";
dataGridViewobat.Columns[6].HeaderText = "Harga Jual";
//dataGridViewobat.Columns[7].HeaderText = "Tanggal Kadaluarsa";
dataGridViewobat.Columns[7].HeaderText = "Stok";
dataGridViewobat.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
private void add(int kode, string nama, string kategori, string golongan, string kemasan,
string harga_beli, string harga_jual, string stok)
{
string sql = "insert into
tbl_obat(kode_obat,nama_obat,kategori,golongan,kemasan,harga_beli,harga_jual,jumlah_stok)
values
(@pkode,@pnama,@pkategori,@pgolongan,@pkemasan,@pharga_beli,@pharga_jual,@pstok)";
cmd = new MySqlCommand(sql,con);
//bikin parameter
cmd.Parameters.AddWithValue("@pkode", kode);
cmd.Parameters.AddWithValue("@pnama", nama);
cmd.Parameters.AddWithValue("@pkategori", kategori);
cmd.Parameters.AddWithValue("@pgolongan", golongan);
cmd.Parameters.AddWithValue("@pkemasan", kemasan);
cmd.Parameters.AddWithValue("@pharga_beli", harga_beli);
cmd.Parameters.AddWithValue("@pharga_jual", harga_jual);
cmd.Parameters.AddWithValue("@pstok", stok);
//buka koneksi dan exec insert
try {
con.Open();
if (MessageBox.Show("Apakah anda ingin menyimpan data ini kedalam database?",
"SIMPAN", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
if (cmd.ExecuteNonQuery() > 0)
{
cleartext();
MessageBox.Show("Successfully insert");
}
con.Close();
retrieve();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
}
private void populate(string kode, string nama, string kategori, string golongan, string
kemasan, string harga_beli, string harga_jual, string stok)
{
dataGridViewobat.Rows.Add(kode, nama, kategori, golongan, kemasan, harga_beli,
harga_jual,stok);
}
private void retrieve() {
dataGridViewobat.Rows.Clear();
// query
string sql = "select
kode_obat,nama_obat,kategori,golongan,kemasan,harga_beli,harga_jual,jumlah_stok from
tbl_obat";
cmd = new MySqlCommand(sql, con);
//buka koneksi
try
{
con.Open();
adapter = new MySqlDataAdapter(cmd);
adapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
populate(row[0].ToString(), row[1].ToString(), row[2].ToString(), row[3].ToString(),
row[4].ToString(), row[5].ToString(), row[6].ToString(), row[7].ToString());
}
con.Close();
//clear dt
dt.Rows.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
private void update(int kode,string nama, string kategori, string golongan, string kemasan,
string harga_beli, string harga_jual, string stok)
{
string sql = "update tbl_obat set nama_obat='" + nama + "' , kategori = '" + kategori +
"', golongan = '" + golongan + "',kemasan = '" + kemasan + "',harga_beli = '" + harga_beli +
"',harga_jual = '" + harga_jual + "',jumlah_stok = '" + stok + "' where kode_obat = '" + kode +
"'";
cmd = new MySqlCommand(sql, con);
try {
con.Open();
adapter = new MySqlDataAdapter(cmd);
adapter.UpdateCommand = con.CreateCommand();
adapter.UpdateCommand.CommandText = sql;
if (MessageBox.Show("Apakah anda ingin mengUpdate data ini dari database?",
"UPDATE", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
if (adapter.UpdateCommand.ExecuteNonQuery() > 0)
{
cleartext();
MessageBox.Show("Successfully update");
}
}
con.Close();
retrieve();
}
catch (Exception ex){
MessageBox.Show(ex.Message);
con.Close();
}
}
private void delete(int kode) {
string sql = "delete from tbl_obat where kode_obat = '"+kode+"'";
cmd = new MySqlCommand(sql, con);
try {
con.Open();
adapter = new MySqlDataAdapter(cmd);
adapter.DeleteCommand = con.CreateCommand();
adapter.DeleteCommand.CommandText = sql;
if (MessageBox.Show("Apakah anda ingin menghapus data ini dari database?",
"DELETE", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
if (cmd.ExecuteNonQuery() > 0) {
cleartext();
MessageBox.Show("Successfully delete");
}
con.Close();
retrieve();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
=
=
=
=
=
=
"";
"";
"";
"";
"";
"";
textBox8.Text = "";
textBox9.Text = "";
}
private void FormMasterObat_Load(object sender, EventArgs e)
{
}
private void button8_Click(object sender, EventArgs e)
{
retrieve();
}
private void button4_Click(object sender, EventArgs e)
{
add(Convert.ToInt32(textBox2.Text), textBox3.Text, textBox4.Text, textBox5.Text,
textBox6.Text, textBox7.Text, textBox8.Text, textBox9.Text);
}
private void dataGridViewObat(object sender, MouseEventArgs e)
{
textBox2.Text = dataGridViewobat.SelectedRows[0].Cells[0].Value.ToString();
textBox3.Text = dataGridViewobat.SelectedRows[0].Cells[1].Value.ToString();
textBox4.Text = dataGridViewobat.SelectedRows[0].Cells[2].Value.ToString();
textBox5.Text = dataGridViewobat.SelectedRows[0].Cells[3].Value.ToString();
textBox6.Text = dataGridViewobat.SelectedRows[0].Cells[4].Value.ToString();
textBox7.Text = dataGridViewobat.SelectedRows[0].Cells[5].Value.ToString();
textBox8.Text = dataGridViewobat.SelectedRows[0].Cells[6].Value.ToString();
textBox9.Text = dataGridViewobat.SelectedRows[0].Cells[7].Value.ToString();
}
e)
delete(kode);
{
}
dataGridViewobat.Rows.Clear();
cleartext();
//autogenerate
//string kodeobat = "select max(kode_obat)+1 from tbl_obat";
//cmd = new MySqlCommand(kodeobat,con);
// con.Open();
//string kode = cmd.ExecuteNonQuery().ToString();
//textBox2.Text = kode.ToString();
//con.Close();
int coffeeID = 0;
string sql = "SELECT MAX(kode_obat) FROM tbl_obat";
//cmd.CommandType = CommandType.Text;
cmd = new MySqlCommand(sql, con);
con.Open();
coffeeID = (int)cmd.ExecuteScalar();
int newCoffeeID = coffeeID + 1;
//string newid = "A00";
textBox2.Text = newCoffeeID.ToString();
textBox2.Enabled = false;
con.Close();
}
using
using
using
using
using
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
MySql.Data.MySqlClient;
namespace Apotik
{
public partial class FormPemasok : Form
{
static string conString = "server=localhost;database=apotik;uid=root;password=";
MySqlConnection con = new MySqlConnection(conString);
MySqlCommand cmd;
MySqlDataAdapter adapter;
DataTable dt = new DataTable();
public FormPemasok()
{
InitializeComponent();
// untuk datagridview property
dataGridViewPemasok.ColumnCount = 6;
dataGridViewPemasok.Columns[0].HeaderText
dataGridViewPemasok.Columns[1].HeaderText
dataGridViewPemasok.Columns[2].HeaderText
dataGridViewPemasok.Columns[3].HeaderText
dataGridViewPemasok.Columns[4].HeaderText
dataGridViewPemasok.Columns[5].HeaderText
=
=
=
=
=
=
"Kode Pemasok";
"Nama Pemasok";
"Alamat";
"Kota";
"Telp";
"Email";
dataGridViewPemasok.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells;
private void add(int kode, string nama, string alamat, string kota, string telp, string email)
{
string sql = "insert into tbl_pemasok(kode_pem,nama_pemasok,alamat,kota,telp,email)
values (@pkode,@pnama,@palamat,@pkota,@ptelp,@pemail)";
cmd = new MySqlCommand(sql, con);
//bikin parameter
cmd.Parameters.AddWithValue("@pkode", kode);
cmd.Parameters.AddWithValue("@pnama", nama);
cmd.Parameters.AddWithValue("@palamat", alamat);
cmd.Parameters.AddWithValue("@pkota", kota);
cmd.Parameters.AddWithValue("@ptelp", telp);
cmd.Parameters.AddWithValue("@pemail", email);
try
{
con.Open();
if (MessageBox.Show("Apakah anda ingin menyimpan data ini kedalam database?",
"SIMPAN", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
if (cmd.ExecuteNonQuery() > 0)
{
cleartext();
MessageBox.Show("Successfully insert");
}
con.Close();
retrieve();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
}
private void populate(string kode, string nama, string alamat, string kota, string telp, string
email)
{
dataGridViewPemasok.Rows.Add(kode, nama, alamat, kota, telp, email);
}
private void retrieve()
{
dataGridViewPemasok.Rows.Clear();
// query
string sql = "select kode_pem,nama_pemasok,alamat,kota,telp,email from tbl_pemasok";
cmd = new MySqlCommand(sql, con);
//buka koneksi
try
{
con.Open();
adapter = new MySqlDataAdapter(cmd);
adapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
populate(row[0].ToString(), row[1].ToString(), row[2].ToString(), row[3].ToString(),
row[4].ToString(), row[5].ToString());
}
con.Close();
//clear dt
dt.Rows.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
private void update(int kode, string nama, string alamat, string kota, string telp, string
email)
{
string sql = "update tbl_pemasok set nama_pemasok='" + nama + "' , alamat = '" +
alamat + "', kota = '" + kota + "',telp = '" + telp + "',email = '" + email + "'";
cmd = new MySqlCommand(sql, con);
try
{
con.Open();
adapter = new MySqlDataAdapter(cmd);
adapter.UpdateCommand = con.CreateCommand();
adapter.UpdateCommand.CommandText = sql;
if (MessageBox.Show("Apakah anda ingin mengUpdate data ini dari database?",
"UPDATE", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
if (adapter.UpdateCommand.ExecuteNonQuery() > 0)
{
cleartext();
MessageBox.Show("Successfully update");
}
}
con.Close();
retrieve();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
}
private void delete(int kode)
{
string sql = "delete from tbl_pemasok where kode_pem = '" + kode + "'";
cmd = new MySqlCommand(sql, con);
try
{
con.Open();
adapter = new MySqlDataAdapter(cmd);
adapter.DeleteCommand = con.CreateCommand();
adapter.DeleteCommand.CommandText = sql;
if (MessageBox.Show("Apakah anda ingin menghapus data ini dari database?",
"DELETE", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
if (cmd.ExecuteNonQuery() > 0)
{
cleartext();
MessageBox.Show("Successfully delete");
}
con.Close();
retrieve();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
con.Close();
}
}
private void cleartext()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
richTextBox1.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
retrieve();
}
delete(kode);
}
private void button1_Click(object sender, EventArgs e)
{
cleartext();
//autogenerate
//string kodeobat = "select max(kode_obat)+1 from tbl_obat";
//cmd = new MySqlCommand(kodeobat,con);
// con.Open();
//string kode = cmd.ExecuteNonQuery().ToString();
//textBox2.Text = kode.ToString();
//con.Close();
int coffeeID = 0;
string sql = "SELECT MAX(kode_pem) FROM tbl_pemasok";
//cmd.CommandType = CommandType.Text;
}
}