TUGAS
PEMROGRAMAN VISUAL II
Disusun
Oleh
Nama : Wahyu Nurfian
NIM : 12.12.0062
Kelas :
SI 12 A
SEKOLAH
TINGGI MANAJEMEN INFORMATIKA DAN KOMPUTER
AMIKOM
PURWOKERTO
JL.LET.JEND.POL
SUMARTO PURWOKERTO
2013
Class
form utama
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;
namespace TesDatSet
{
Public
partial class Form1 : Form
{
public frmmatakuliah()
{
InitializeComponent();
}
Private
void frmmatakuliah_Load(object
sender, EventArgs e)
{
noSKS();
noSemester();
tampilData();
}
Private
void cmdSimpan_Click(object
sender, EventArgs e)
{
simpanData();
}
Private
void cmdEdit_Click(object
sender, EventArgs e)
{
ubahData();
}
Private
void cmdHapus_Click(object
sender, EventArgs e)
{
hapusData();
}
Private
void cmdReset_Click(object
sender, EventArgs e)
{
empty();
}
Private
void cmdTutup_Click(object
sender, EventArgs e)
{
Application.Exit();
}
Private
void dataGridView1_CellClick(object
sender, DataGridViewCellEventArgs e)
{
//seleksiBaris();
txtKode.Text = (string)dataGridView1["kode_matkul",
e.RowIndex].Value.ToString();
txtNama.Text = (string)dataGridView1["nama_matkul",
e.RowIndex].Value.ToString();
cmbSks.SelectedItem = (byte)dataGridView1["sks",
e.RowIndex].Value;
txtPrasyarat.Text = (string)dataGridView1["prasyarat",
e.RowIndex].Value.ToString();
cmbSemester.SelectedItem = (byte)dataGridView1["semester",
e.RowIndex].Value;
}
Private
void txtCari_TextChanged(object
sender, EventArgs e)
{
cariData();
}
//
Method
void noSKS()
{
cmbSks.Items.Add("");
for (byte i = 1; i <= 6; i++)
{
cmbSks.Items.Add(i);
}
}
void noSemester()
{
cmbSemester.Items.Add("");
for (byte i = 1; i <= 8; i++)
{
cmbSemester.Items.Add(i);
}
}
void empty()
{
txtKode.Clear();
txtNama.Clear();
cmbSks.SelectedItem = "";
txtPrasyarat.Clear();
cmbSemester.SelectedItem = "";
txtKode.Focus();
}
void tampilData()
{
string query = "select * from matakuliah order
by kode_matkul asc";
matakuliahManager MM = new matakuliahManager();
MM.TampilData(query, "matakuliah", dataGridView1);
}
void simpanData()
{
matakuliah m = new matakuliah(txtKode.Text, txtNama.Text, int.Parse(cmbSks.Text), txtPrasyarat.Text, int.Parse(cmbSemester.Text));
//matakuliah
m = new matakuliah(txtKode.Text, txtNama.Text, cm);
matakuliahManager mm = new matakuliahManager();
int x = mm.add(m);
if (x == 1)
{
MessageBox.Show("Data berhasil
disimpan !");
empty();
tampilData();
}
else
{
MessageBox.Show("Data gagal
disimpan !");
}
}
void hapusData()
{
matakuliah m = new matakuliah(txtKode.Text);
matakuliahManager mm = new matakuliahManager();
int x = mm.delete(m);
if (txtKode.Text == "")
{
MessageBox.Show("Pilih dahulu data
yang akan dihapus");
}
else
{
if (x == 1)
{
MessageBox.Show("Hapus data
berhasil");
empty();
tampilData();
}
else
{
MessageBox.Show("Hapus data
gagal");
}
}
}
void ubahData()
{
matakuliah m = new matakuliah(txtKode.Text, txtNama.Text, int.Parse(cmbSks.Text), txtPrasyarat.Text, int.Parse(cmbSemester.Text));
matakuliahManager mm = new matakuliahManager();
int x = mm.ubah(m);
try
{
if (x == 1)
{
MessageBox.Show("Ubah data
berhasil !");
empty();
tampilData();
}
else
{
MessageBox.Show("Ubah data gagal !");
}
}
catch (Exception EX)
{
MessageBox.Show(EX.Message);
}
}
void cariData()
{
string query = "select* from matakuliah where
nama_matkul like '%" + txtCari.Text + "%'";
matakuliahManager m = new matakuliahManager();
m.TampilData(query, "matakuliah", dataGridView1);
Class
Koneksi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace TesDataSet
{
Class
koneksi
{
Static
string conn;
Public
static SqlConnection Conn
{
get
{
Return
new SqlConnection(conn);
}
}
static koneksi()
{
string connStr = "data source=VIENA; initial
catalog=db_matakuliah; integrated security=true;";
conn = connStr;
}
}
}
Class
Fungsi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace TesDataSet
{
Class
fungsi
{
Static
SqlConnection conn;
Static
SqlCommand cmd;
Static
SqlDataAdapter da;
Static
DataSet ds;
Public
staticint EQuery(string query)
{
conn = koneksi.Conn;
cmd = new SqlCommand();
try
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = query;
int i = cmd.ExecuteNonQuery();
return i;
}
catch (SqlException ex)
{
Console.WriteLine(ex.ToString());
return 0;
}
finally
{
conn.Close();
}
}
publicstaticDataSet DataGrid(string query, string
tabel)
{
conn = koneksi.Conn;
da = newSqlDataAdapter(query, conn);
ds = newDataSet();
try
{
conn.Open();
da.Fill(ds, tabel);
return ds;
}
catch (SqlException e)
{
MessageBox.Show(e.Message);
returnnull;
}
finally
{
conn.Close();
}
}
}
}
Class
Matakuliah
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TesDataSet
{
Class
matakuliah
{
string kode, nama, prasyarat;
public
string Prasyarat
{
get { return prasyarat; }
set { prasyarat = value; }
}
Public
string Nama
{
get { return nama; }
set { nama = value; }
}
Public
string Kode
{
get { return kode; }
set { kode = value; }
}
int sks, semester;
public
int Semester
{
get { return semester; }
set { semester = value; }
}
Public
int Sks
{
get { return sks; }
set { sks = value; }
}
//constructor
public matakuliah(string kode, string nama, int sks,
string prasyarat, int
semester)
{
this.kode = kode;
this.nama = nama;
this.prasyarat = prasyarat;
this.sks = sks;
this.semester = semester;
}
public matakuliah(string kode)
{
this.kode = kode;
}
}
}
Class
Matakuliah Manager
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace TesDataSet
{
Class
matakuliahManager
{
Public
void TampilData(string query,
string tabel, DataGridView
DG)
{
try
{
DG.DataSource = fungsi.DataGrid(query, tabel);
DG.DataMember = tabel;
DG.Columns[0].HeaderText = "Kode";
DG.Columns[1].HeaderText = "Nama Matakuliah";
DG.Columns[2].HeaderText = "SKS";
DG.Columns[3].HeaderText = "Prasyarat";
DG.Columns[4].HeaderText = "Semester";
DG.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
DG.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
DG.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
DG.AutoResizeColumns();
DG.AutoResizeColumnHeadersHeight();
DG.AlternatingRowsDefaultCellStyle.BackColor = Color.Blue;
}
catch (Exception e)
{
MessageBox.Show("Error : "
+ e.Message);
}
finally
{
}
}
Public
int add(matakuliah mk)
{
string query;
if (mk.Prasyarat.ToString() == "")
{
query = "insert into matakuliah values('" + mk.Kode + "','" + mk.Nama + "',"
+ mk.Sks + ",null,"
+ mk.Semester + ");";
}
else
{
query = "insert into matakuliah values('" + mk.Kode + "','"
+ mk.Nama + "'," + mk.Sks + ",'"
+ mk.Prasyarat + "'," +
mk.Semester + ");";
}
try
{
int x = fungsi.EQuery(query);
return x;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return 0;
}
}
publicint delete(matakuliah mkk)
{
string query;
query = "
delete from matakuliah where kode_matkul='" + mkk.Kode + "'";
try
{
int x = fungsi.EQuery(query);
return x;
}
catch (Exception ex)
{
return 0;
}
}
Public
int ubah(matakuliah mk)
{
string query;
if (mk.Prasyarat == null)
{
query = "update matakuliah set nama_matkul = '" + mk.Nama + "',sks = " + mk.Sks + ",prasyarat = '" + mk.Prasyarat + "',semester = " + mk.Semester + " where kode_matkul = '" + mk.Kode + "'";
}
else
{
query = "update matakuliah set nama_matkul = '" + mk.Nama + "',sks = " + mk.Sks + ",prasyarat = null,semester = " +
mk.Semester + " where kode_matkul = '"
+ mk.Kode + "'";
}
try
{
int x = fungsi.EQuery(query);
return x;
}
catch (Exception ex)
{
return 0;
}
}
}
}
Hasil Outpunya :