Using Using Using Using Namespace Static Class

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

Program.

cs

// class C# yang di jalankan pertama kali

//memanggil library2 pada C#, kalau di java = import bla.bla.bla


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Apotik
//namespace kalau dijava sama dengan project/workspace
{
static class Program//Program adalah nama classnya
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new LoginForm());
//LoginForm adalah form yang dipanggil pertama
kali
}
}
}

FormLogin.cs //class FormLogin yang di panggil pertama kali


//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 aplikasinya
{
public partial class LoginForm : Form
{
public LoginForm()
//function yg dipanggil di class program.cs
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{

//ini adalah button Login

//bikin string untuk tampung nilai


string usr = usrname.Text.ToString();
string psw = pssword.Text.ToString();
string id_usr = "";
string nm_usr = "";
string username = "";
string koneksi = "server=localhost;database=apotik;uid=root;password=";
//--------------------------------//panggil fungsi C# mysqlconnection,mysqlcommand,mysqldataadapter,datatable
MySqlConnection koneksiB = new MySqlConnection(koneksi);
MySqlCommand com = new MySqlCommand("select * from tbl_user where
username='" + usr + "' and password='" + psw + "'", koneksiB);
MySqlDataAdapter adap = new MySqlDataAdapter(com);
DataTable table = new DataTable();
adap.Fill(table);
koneksiB.Open();
foreach (DataRow kolom in table.Rows)
{
id_usr = kolom["id_user"].ToString();
//nm_usr = kolom[""].ToString();
username = kolom["username"].ToString();
}

//validasi jika username/usr,password/psw masih kosong, selain itu jika rows


didalam table lebih besar dari 0 maka jalankan atau tampilkan FormApotik.
if (usr == "") {
MessageBox.Show("Username harus diisi");
}else if(psw ==""){
MessageBox.Show("Password harus diisi");
}else if (table.Rows.Count > 0)
{
FormApotik f1 = new FormApotik();
f1.id_user = id_usr;
f1.namaUser = nm_usr;
f1.userName = username;
f1.Show(); //tampilkan atau show FormApotik
this.Hide(); //artinya tutup form ini/form Login
}
else
{
MessageBox.Show("Username atau password yang anda masukkan salah");
}
koneksiB.Close();
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}

}
}

FormMasterObat.cs//class atau form yg dipanggil jika memilih menu master obat

//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;

//untuk selection mode


dataGridViewobat.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridViewobat.MultiSelect = false;

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();
}

private void cleartext() {


textBox2.Text
textBox3.Text
textBox4.Text
textBox5.Text
textBox6.Text
textBox7.Text

=
=
=
=
=
=

"";
"";
"";
"";
"";
"";

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)

private void dataGridViewobat_CellContentClick(object sender, DataGridViewCellEventArgs


{
}
private void button2_Click(object sender, EventArgs e)
{
string selected = dataGridViewobat.SelectedRows[0].Cells[0].Value.ToString();
int kode = Convert.ToInt32(selected);

update(kode, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text,


textBox8.Text, textBox9.Text);
}
private void button3_Click(object sender, EventArgs e)
{
string selected = dataGridViewobat.SelectedRows[0].Cells[0].Value.ToString();
int kode = Convert.ToInt32(selected);
}

delete(kode);

private void button5_Click(object sender, EventArgs e)

{
}

dataGridViewobat.Rows.Clear();
cleartext();

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_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();
}

FormPemasok.cs //class master pemasok


//panggil library2 C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

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;

//untuk selection mode


dataGridViewPemasok.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridViewPemasok.MultiSelect = false;

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);

//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 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();
}

private void button4_Click(object sender, EventArgs e)


{
add(Convert.ToInt32(textBox1.Text), textBox2.Text,richTextBox1.Text, textBox3.Text,
textBox4.Text, textBox5.Text);
}
private void button2_Click(object sender, EventArgs e)
{
string selected = dataGridViewPemasok.SelectedRows[0].Cells[0].Value.ToString();
int kode = Convert.ToInt32(selected);
update(kode, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text,
richTextBox1.Text);
}
private void button3_Click(object sender, EventArgs e)
{
string selected = dataGridViewPemasok.SelectedRows[0].Cells[0].Value.ToString();
int kode = Convert.ToInt32(selected);
}

delete(kode);

private void button5_Click(object sender, EventArgs e)


{
dataGridViewPemasok.Rows.Clear();
cleartext();
}
private void dataGridViewPemasok_click(object sender, MouseEventArgs e)
{
textBox1.Text = dataGridViewPemasok.SelectedRows[0].Cells[0].Value.ToString();
textBox2.Text = dataGridViewPemasok.SelectedRows[0].Cells[1].Value.ToString();
textBox3.Text = dataGridViewPemasok.SelectedRows[0].Cells[3].Value.ToString();
textBox4.Text = dataGridViewPemasok.SelectedRows[0].Cells[4].Value.ToString();
textBox5.Text = dataGridViewPemasok.SelectedRows[0].Cells[5].Value.ToString();
richTextBox1.Text = dataGridViewPemasok.SelectedRows[0].Cells[2].Value.ToString();

}
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;

cmd = new MySqlCommand(sql, con);


con.Open();
coffeeID = (int)cmd.ExecuteScalar();
int newCoffeeID = coffeeID + 1;
//string newid = "A00";
textBox1.Text = newCoffeeID.ToString();
textBox1.Enabled = false;
con.Close();
}
private void button6_Click(object sender, EventArgs e)
{

}
}

You might also like