Sabtu, 14 Mei 2011
Contoh koneksi untuk ke Database 3
Public Class Form2
Sub Kosong()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.Focus()
End Sub
Sub netral()
Bttambah.Text = "&Tambah"
BtUbah.Text = "&Ubah"
Bthapus.Text = "&Hapus"
btkeluar.Text = "&Keluar"
Bttambah.Enabled = True
BtUbah.Enabled = True
Bthapus.Enabled = True
TextBox1.Enabled = True
End Sub
Sub cekrecord()
Call mykoneksi()
Tampil.Connection = DataAdit
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "select * from Jurusan where KdJur='" & Trim(TextBox1.Text) & "'"
Tampilkan = Tampil.ExecuteReader
If Tampilkan.HasRows = True Then
hasilcek = True
Else
hasilcek = False
End If
End Sub
Private Sub BtnTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bttambah.Click
Call simpankelompok()
'memenggil metod (simpankelompok)
End Sub
Sub simpankelompok()
If Bttambah.Text = "&Tambah" Then
TextBox1.Text = ""
Bttambah.Text = "Simpan"
BtUbah.Enabled = False
Bthapus.Enabled = False
btkeluar.Text = "&Batal"
'TextBox1.Enabled = False
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub
If TextBox2.Text = "" Then Exit Sub
Call cekrecord()
Try
If hasilcek = "False" Then 'jika data true langsung ke else
Call mykoneksi()
Tampil.Connection = DataAdit
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "Insert Into Jurusan (KdJur,NmJur)values( '" & TextBox1.Text & "','" & TextBox2.Text & "')"
Tampil.ExecuteNonQuery()
Call Kosong()
Else
MsgBox("Data sudah ada boss, silahkan cek data yang di input", MsgBoxStyle.Critical, "Input data")
TextBox1.Focus()
End If
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End If
End Sub
Private Sub BtnKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btkeluar.Click
If btkeluar.Text = "&Keluar" Then
Me.Close()
Else
Call Kosong()
Call netral()
End If
End Sub
Private Sub Btnubah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtUbah.Click
Call Btnubahkelompok()
End Sub
Sub Btnubahkelompok()
If BtUbah.Text = "&Ubah" Then
TextBox1.Text = ""
BtUbah.Text = "Simpan"
Bttambah.Enabled = False 'enabled= aktif/tidak aktif
Bthapus.Enabled = False
btkeluar.Text = "&Batal"
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub 'untuk mengeck data ada/tidak lalu keluar, dan tidak akan dijalankan
If TextBox2.Text = "" Then Exit Sub
Try
Call mykoneksi() 'untuk mengecek data kdjur ada apa tidak
Tampil.Connection = DataAdit
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "Update Jurusan Set NmJur ='" & Trim(TextBox2.Text) & "' where KdJur ='" & Trim(TextBox1.Text) & "'"
Tampil.ExecuteNonQuery()
Call Kosong()
TextBox1.Focus()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End If
End Sub
Private Sub BtnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bthapus.Click
Call BtnHapuskelompok()
End Sub
Sub BtnHapuskelompok()
If Bthapus.Text = "&Hapus" Then
TextBox1.Text = ""
Bthapus.Text = "Ok"
Bttambah.Enabled = False
BtUbah.Enabled = False
btkeluar.Text = "&Batal"
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub
If TextBox2.Text = "" Then Exit Sub
Try
Call mykoneksi()
Tampil.Connection = DataAdit
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "Delete from Jurusan where KdJur='" & Trim(TextBox1.Text) & "'"
Tampil.ExecuteNonQuery()
Call Kosong()
TextBox1.Focus()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(Keys.Enter) Then
If Bttambah.Text = "Simpan" Then
Call cekrecord()
If hasilcek = False Then
TextBox2.Focus()
Else
MsgBox("Kode sudah ada boss, silahkan ganti kode lain boss", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Infromasi")
TextBox1.Focus()
End If
Else
Call mykoneksi()
Tampil.Connection = DataAdit
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "select * from Jurusan where kdjur='" & Trim(TextBox1.Text) & "'"
Tampilkan = Tampil.ExecuteReader
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
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar