Imports System.Data.SqlClient
Public Class FrmPendaftaran
Sub opentable()
cmd.Connection = cn
cmd.CommandText = "SELECT * FROM tb_pendaftaran"
Dim rd As SqlDataReader = cmd.ExecuteReader
Dim dt As New DataTable
dt.Load(rd)
DataGridView1.DataSource = dt
End Sub
Sub bersih()
txtid.Text = ""
txtnama.Text = ""
txttempatlahir.Text = ""
tgl_lahir.Text = ""
txtasalsekolah.Text = ""
txtalamat.Text = ""
End Sub
Sub kodeotomatis()
Dim kodeauto As Single
cmd.Connection = cn
cmd.CommandText = "SELECT COUNT(*) as id_pendaftaran FROM tb_pendaftaran"
Dim rd As SqlDataReader = cmd.ExecuteReader
While rd.Read
kodeauto = Val(rd.Item("id_pendaftaran").ToString) + 1
End While
Select Case Len(Trim(kodeauto))
Case 1 : txtid.Text = "IP00" + Trim(Str(kodeauto))
Case 1 : txtid.Text = "IP0" + Trim(Str(kodeauto))
Case 1 : txtid.Text = "IP" + Trim(Str(kodeauto))
End Select
rd.Close()
cn.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End
End Sub
Private Sub FrmPendaftaran_Load(sender As Object, e As EventArgs) Handles MyBase.Load
opendb()
opentable()
kodeotomatis()
End Sub
Private Sub btninsert_Click(sender As Object, e As EventArgs) Handles btninsert.Click
cn.Open()
cmd.Connection = cn
cmd.CommandText = "INSERT INTO tb_pendaftaran VALUES ('" & txtid.Text & "','" & txtnama.Text & "','" & txttempatlahir.Text & "','" & tgl_lahir.Value & "','" & txtasalsekolah.Text & "','" & txtalamat.Text & "')"
cmd.ExecuteNonQuery()
MessageBox.Show("Data Berhasil Tersimpan")
bersih()
End Sub
Private Sub DataGridView1_DoubleClick(sender As Object, e As EventArgs) Handles DataGridView1.DoubleClick
txtid.Text = DataGridView1.SelectedCells(0).Value
txtnama.Text = DataGridView1.SelectedCells(1).Value
txttempatlahir.Text = DataGridView1.SelectedCells(2).Value
tgl_lahir.Text = DataGridView1.SelectedCells(3).Value
txtasalsekolah.Text = DataGridView1.SelectedCells(4).Value
txtalamat.Text = DataGridView1.SelectedCells(5).Value
End Sub
Private Sub btnupdate_Click(sender As Object, e As EventArgs) Handles btnupdate.Click
cmd.Connection = cn
cmd.CommandText = "UPDATE tb_pendaftaran SET nama = '" & txtnama.Text & "', tempat_lahir = '" & txttempatlahir.Text & "', tanggal_lahir = '" & tgl_lahir.Value & "', asal_sekolah = '" & txtasalsekolah.Text & "', alamat_pendaftar = '" & txtalamat.Text & "' where id_pendaftaran = '" & txtid.Text & "'"
cmd.ExecuteNonQuery()
MessageBox.Show("Data Berhasil Terubah")
bersih()
End Sub
Private Sub btnrefresh_Click(sender As Object, e As EventArgs) Handles btnrefresh.Click
opentable()
End Sub
Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
Dim baris As Integer
Dim id As String
baris = DataGridView1.CurrentCell.RowIndex
id = DataGridView1(0, baris).Value.ToString
cmd.Connection = cn
cmd.CommandText = "DELETE FROM tb_pendaftaran WHERE id_pendaftaran = '" + id + "'"
cmd.ExecuteNonQuery()
MessageBox.Show("Data Berhasil Terhapus")
opentable()
End Sub
Private Sub FrmPendaftaran_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
cn.Open()
End Sub
Private Sub txtasalsekolah_TextChanged(sender As Object, e As EventArgs) Handles txtasalsekolah.TextChanged
End Sub
End Class