Jadikan hidup lebih bermakna.... Penuh semangat dan motivasi.... Wujudkan diri dengan karya dan bukti nyata.............................. Jadilah inspirasi untuk anak negeri....Kebanggaan diri sendiri.... Kebanggaan Keluarga.... Kebanggaan Bangsa dan Negara....
INDRA YUSUF_gcoz.blogspot

Jumat, 27 Mei 2011

Program5. Vb.Net (Latihan Materi)


Dalam membuat program kita biasa menggunakan database.
Sebagai koneksi kita menggunakan module yang dibuat untuk mengkoneksikan dengan database

Sintaxnya sebagai berikut :

Imports System.Data
Imports System.Data.Sql
Module koneksi
Public database As New OleDb.OleDbConnection
Public tampil2 As New OleDb.OleDbCommand
Public tampilkan As OleDb.OleDbDataReader
Public hasilcek As String

Public Sub bukakoneksi()
database.Close()
Try
database.ConnectionString = "provider=sqloledb.1;integrated security=SSPI;persist security info=false; initial catalog=cobasql;data source=."
database.Open()
MessageBox.Show("Koneksi kedatabase berhasil")
Catch ex As Exception
MessageBox.Show("koneksi gagal")
End
End Try
End Sub
End Module

Tampilan formnya sebagai berikut :


Kemudian membuat sintaxnya sebagai berikut :

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call bukakoneksi()

End Sub
Sub Jurusan()
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
While Tampilkan.Read()
If IsDBNull(tampilkan("kdjur")) Then
Else
TextBox2.Text = tampilkan("nmjur")
End If
End While
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call Jurusan()

End Sub
End Class

Kemudian buat tampilan form ke-2 sebagai berikut :

dan sintaxnya sebagai berikut :

Public Class Form2
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
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call simpankelompok()
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 Button4_Click(ByVal sender As System.Object, ByVal e As System.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 As System.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 As System.EventArgs) Handles Button3.Click
Call hapuskelompok()

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

jangan lupa dibuat database terlebih dahulu dengan menggunakan SQL Server 2000.
untuk data yang kita ambil nantinya..

demikian program yang kita buat kali ini...
dilanjutkan nanti ke program cristal report di pertemuan selanjutnya
untuk pembuatan laporannya...

selamat mencoba...


.

0 komentar:

Posting Komentar

INDRA YUSUF_gcoz.blogspot

B.Inggris

Join yuuu...

BISNIS ONLINE

Bisnis 100% Tanpa Modal

idebagus

Web hosting dan domain gratis untuk website di IdeBagus

BISNIS GRATIS