Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Imports MySql.Data.

MySqlClient

Public Class Form1


Sub ketemu()
Dim baca As MySqlDataReader
cmd.Connection = kOn
cmd.CommandText = "SELECT * FROM tbl_mhs where nim ='" & TextBox1.Text & "'"
baca = cmd.ExecuteReader()
Try
While baca.Read
TextBox2.Text = baca.GetString("nm_mhs")
TextBox3.Text = baca.GetString("alamat")
TextBox4.Text = baca.GetString("ttl")
ComboBox1.Text = baca.GetString("jk")
End While
Catch ex As Exception
End Try
baca.Close()
End Sub
Sub kosong()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
ComboBox1.Text = ""
TextBox1.Focus()
End Sub
Sub cari()
Dim dt As New DataTable
cmd.Connection = kOn
cmd.CommandText = "SELECT * FROM tbl_mhs where Nim ='" & TextBox1.Text & "'"
baca = cmd.ExecuteReader()
' baca.Close()
End Sub
Sub TampilDb()
Dim dt As New DataTable
Try
cmd.Connection = kOn
cmd.CommandText = "SELECT * FROM tbl_mhs "
adapt.SelectCommand = cmd
adapt.Fill(dt)
Tabel.DataSource = dt
With Tabel
.RowHeadersVisible = False
.AllowUserToAddRows = False
.ReadOnly = True
.Refresh()
End With
Catch ex As Exception
MsgBox("Terjadi Kesalahan", vbCritical, "Error")
Exit Sub
End Try
adapt.Dispose()
End Sub

Sub combo()
ComboBox1.Items.Add("Laki-Laki")
ComboBox1.Items.Add("Perempuan")
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load
Bukadb()
TampilDb()
combo()
End Sub

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.DataGridViewCellEventArgs)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button1.Click
'Try
If TextBox1.Text = "" Then
MsgBox("Data Belum Lengkap")
Else
Bukadb()
Call cari()
End If
If Not baca.HasRows Then
baca.Close()
With cmd
.Connection = kOn
.CommandText = "Insert into tbl_mhs(nim,nm_mhs,alamat,ttl,jk) values " &
_
"('" & TextBox1.Text & "','" & TextBox2.Text & "','" &
TextBox3.Text & "','" & _
TextBox4.Text & "','" & ComboBox1.Text & "' )"
.ExecuteNonQuery()
MessageBox.Show("Insert Data Mahasiswa Berhasil Dilakukan", "Pesan",
MessageBoxButtons.OK)
kosong()
End With
Call TampilDb()

Else
baca.Close()
cmd.Connection = kOn
cmd.CommandText = "update tbl_mhs set " & _
"nm_mhs = '" & TextBox2.Text & "' ," & _
"alamat = '" & TextBox3.Text & "', " & _
"ttl = '" & TextBox4.Text & "', " & _
"jk = '" & ComboBox1.Text & "' " & _
" where nim ='" & TextBox1.Text & "'"
cmd.ExecuteNonQuery()
MessageBox.Show("data sudah diubah", "Ubah Data",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Call TampilDb()
kosong()
Exit Sub
End If

' Catch ex As Exception

'End Try
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button3.Click
If TextBox1.Text <> "" Then
Dim Hapus = MessageBox.Show("Yakin Data " & TextBox1.Text & "Akan Dihapus ?",
"Hapus Data", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
If Hapus = DialogResult.Yes Then
cmd.Connection = kOn
cmd.CommandText = "delete from tbl_mhs where nim ='" & TextBox1.Text &
"'"
cmd.ExecuteNonQuery()
TampilDb()
kosong()
End If

End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button2.Click
kosong()
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button4.Click
Me.Dispose()
End Sub

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)


Handles TextBox1.LostFocus
ketemu()
End Sub
End Class

You might also like