Pada kesempatan ini kita akan mencoba membuat input data, dan langsung tersimpan dalam data base dan menampilkan hasil yang telah kita input. Namun sebelumnya, buatlah dulu sebuah database dlm sql 2000 sesuai yg dibutuhkan, pada contoh ini database yg digunakan sama dengan bahasan sebelumnya, dengan tambahan :
table Jurusan>>berisi colum >>kdjur(kode jurusan) dan >>nmjur(nama jurusan)
Buat form berikut:
Kemudian kita kasih sintaknya :
Public Class update
Sub kosong()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.Focus()
End Sub
Sub netral()
Button1.Text = "Tambah"
Button2.Text = "Ubah"
Button3.Text = "Hapus"
Button4.Text = "Keluar"
Button1.Enabled = True 'enable=funsi aktif/tdaknya sebuah objek
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
End Sub
Sub celrecord()
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = "select * from jurusan where kdjur='" & Trim(TextBox1.Text) & "'"
tampilkan = tampil2.ExecuteReader
If tampilkan.HasRows = True Then ''ada atau tidak recod yg dimaksukkan
hasilcek = True
Else
hasilcek = False
End If
End Sub
Sub simpankelompok()
If Button1.Text = "Tambah" Then
TextBox1.Text = ""
Button1.Text = "Simpan"
Button2.Enabled = False
Button3.Enabled = False
Button4.Text = "Batal"
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub ''then exit sub = keluar dari sub class dgan syarat textbox harus diisi/pengecekan validasi
If TextBox2.Text = "" Then Exit Sub
Call celrecord()
Try
If hasilcek = "False" Then
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = " Insert into Jurusan (kdjur,nmjur)values ('" & TextBox1.Text & "','" & TextBox2.Text & "')"
tampil2.ExecuteNonQuery()
Call kosong()
Else
MsgBox("Data sudah ada , silahkan cek data yang akan diinput ", MsgBoxStyle.Critical, "Input data") ''critical = ! dalam msgbox
End If
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End If
End Sub
Private Sub update_Load(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click
Call simpankelompok()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button4.Click
If Button4.Text = "Keluar" Then
Me.Close()
Else
Call kosong()
Call netral()
End If
End Sub
Sub ubahkelompok()
If Button2.Text = "Ubah" Then
TextBox1.Text = ""
Button2.Text = "Simpan"
Button1.Enabled = False
Button3.Enabled = False
Button4.Text = "Batal"
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub ''then exit sub = keluar dari sub class dgan syarat textbox harus diisi/pengecekan validasi
If TextBox2.Text = "" Then Exit Sub
Try
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = " Update Jurusan set nmjur='" & TextBox2.Text &"'where kdjur='" & TextBox1.Text & "'"
tampil2.ExecuteNonQuery()
Call kosong()
TextBox1.Focus()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button2.Click
Call ubahkelompok()
End Sub
Sub hapuskelompok()
If Button3.Text = "Hapus" Then
TextBox1.Text = ""
Button3.Text = "Ok"
Button1.Enabled = False
Button2.Enabled = False
Button4.Text = "Batal"
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub
If TextBox2.Text = "" Then Exit Sub
Try
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = " Delete from Jurusan where kdjur='" & Trim(TextBox1.Text) & "'"
tampil2.ExecuteNonQuery()
Call kosong()
TextBox1.Focus()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button3.Click
Call hapuskelompok()
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e AsSystem.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(Keys.Enter) Then
If Button1.Text = "Simpan" Then
Call celrecord()
If hasilcek = False Then
TextBox2.Focus()
Else
MsgBox("Kode Sudah ada, silahkan ganti kode lain", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Informasi")
tampilkan = tampil2.ExecuteReader
End If
Else
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = " select* from Jurusan where kdjur='" & Trim(TextBox1.Text) & "'"
tampil2.ExecuteNonQuery()
If tampilkan.HasRows = True Then
While tampilkan.Read()
If (IsDBNull(tampilkan("kdjur"))) Then
TextBox1.Focus()
Else
TextBox2.Text = tampilkan("nmjur")
TextBox2.Focus()
End If
End While
Else
MsgBox("Data tidak diketemukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Tambah Data")
End If
End If
End If
End Sub
End Class
0 komentar:
Posting Komentar