0% found this document useful (0 votes)
77 views9 pages

Aplication: SMK Telkom Sandhy Putra Malang

This document describes a student data application created in VB.net that allows users to input, search, update, and delete student records from a database. The application includes forms to input student name, address, date of birth, and photo. It connects to a MySQL database and allows storing, retrieving, and modifying student records and associated photo files. The code details functions for inserting, updating, deleting records and loading/saving student photos to and from the database.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views9 pages

Aplication: SMK Telkom Sandhy Putra Malang

This document describes a student data application created in VB.net that allows users to input, search, update, and delete student records from a database. The application includes forms to input student name, address, date of birth, and photo. It connects to a MySQL database and allows storing, retrieving, and modifying student records and associated photo files. The code details functions for inserting, updating, deleting records and loading/saving student photos to and from the database.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

SMK TELKOM SANDHY PUTRA MALANG

VB.net
Aplication
Form Data Siswa dengan Load Photo

Frans Caisar Ramadhan


3TKJ1/31
SMK TELKOM SANDHY PUTRA MALANG
2009/2010

Form

1. Proses Input Data Baru

Ketikan nis,nama,alamat,tanggal lahir cari fotonya kemudian klik simpan dan data akan
tersimpan.

2. Proses Cari Data dan Update Data

Cari data dengan mengklik tombol-tombol navigasi yang tersedia setelah mencari bisa
melakukan edit data lalu klik tombol ubah untuk menyimpan update data.
3. Proses Delete Data

Tentukan data yang akan dihapus klik tombol hapus. Akan muncul konfirmasi. Yes untuk
melanjutkan dan no untuk cancel.

Coding
Imports System.IO

Imports System.Drawing.Bitmap

Imports System.Globalization

Public Class Form1

Private strImageName, status As String


Private imageBytes As Byte = Nothing
Private FileSize As UInt32
Private rawData() As Byte
Private fs As FileStream
Private CN As New mySqlConnection
Dim index, indexFirst, indexLast, countData As Integer

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


Handles MyBase.Load
conditionAtShow()
index = 0
ShowData()
If countData = 0 Then
btBrowse.Enabled = False
btDelete.Enabled = False
btEdit.Enabled = False
End If
End Sub

Private Sub conditionAtModify()


btAdd.Enabled = False
btEdit.Enabled = False
btDelete.Enabled = False
btMoFirst.Enabled = False
btMoPrev.Enabled = False
btMoNext.Enabled = False
btMoLast.Enabled = False
btBrowse.Enabled = False
btSave.Enabled = True
btCancel.Enabled = True
btBrowsePicture.Enabled = True
txtNIS.Focus()
End Sub

Private Sub conditionAtShow()


txtNIS.Enabled = True
btAdd.Enabled = True
btEdit.Enabled = True
btDelete.Enabled = True
'btMoFirst.Enabled = True
'btMoPrev.Enabled = True
'btMoNext.Enabled = True
'btMoLast.Enabled = True
btBrowse.Enabled = True
btSave.Enabled = False
btCancel.Enabled = False
btBrowsePicture.Enabled = False
End Sub

Private Sub countDataInDatabase()


Dim myConnectionString As String = "Database=siswa;Data Source=localhost;User
Id=root;Password="
CN = New MySqlConnection(myConnectionString)
CN.Open()

Dim DataReader As MySqlDataReader = Nothing


Dim CMDCount As MySqlCommand = CN.CreateCommand()
Try
Dim Sql As String = "SELECT COUNT(*) as jumlah FROM siswa;"
CMDCount.CommandText = Sql
DataReader = CMDCount.ExecuteReader()
If DataReader.Read() Then
countData = Int(DataReader("jumlah"))
End If

Catch ex As MySqlException
MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
CMDCount.Dispose()
CN = Nothing
End Try
End Sub

Private Sub insertFoto()


fs = New FileStream(strImageName, FileMode.Open, FileAccess.Read)
FileSize = fs.Length
rawData = New Byte(FileSize) {}
fs.Read(rawData, 0, FileSize)
fs.Close()
End Sub

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


Handles btSave.Click
If txtNIS.Text <> "" And txtNama.Text <> "" And txtAlamat.Text <> "" And
txtTanggal.Value.ToString <> "" And strImageName <> "" Then
Dim myConnectionString = "Database=siswa;Data Source=localhost;User
Id=root;Password="
CN = New MySqlConnection(myConnectionString)
CN.Open()
insertFoto()
If status = "add" Then
Dim CMDInsert As MySqlCommand = CN.CreateCommand
Try
Dim Sql As String = "INSERT INTO siswa(nis, nama, alamat, tgl_lahir, foto)"
Sql &= "VALUES (@nis, @nama, @alamat, @tgl_lahir, @foto);"
With CMDInsert
.CommandText = Sql
.Connection = CN
.Parameters.Add("@nis", MySqlDbType.String, 10).Value = txtNIS.Text
.Parameters.Add("@nama", MySqlDbType.VarChar, 50).Value = txtNama.Text
.Parameters.Add("@alamat", MySqlDbType.VarChar, 50).Value = txtAlamat.Text
.Parameters.Add("@tgl_lahir", MySqlDbType.Date).Value =
txtTanggal.Value.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo)
.Parameters.Add("@foto", MySqlDbType.Blob).Value = rawData
.ExecuteNonQuery()
End With
conditionAtShow()
index = 0
ShowData()
Catch ex As MySqlException
MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Finally
CMDInsert.Dispose()
CN = Nothing
End Try
ElseIf status = "edit" Then
Dim CMDUpdate As MySqlCommand = CN.CreateCommand
Try
Dim Sql As String = "UPDATE siswa SET nama=@nama, alamat=@alamat,
tgl_lahir=@tgl_lahir, foto=@foto WHERE nis=@nis;"
With CMDUpdate
.CommandText = Sql
.Connection = CN
.Parameters.Add("@nis", MySqlDbType.String, 10).Value = txtNIS.Text
.Parameters.Add("@nama", MySqlDbType.VarChar, 50).Value = txtNama.Text
.Parameters.Add("@alamat", MySqlDbType.VarChar, 50).Value = txtAlamat.Text
.Parameters.Add("@tgl_lahir", MySqlDbType.Date).Value =
txtTanggal.Value.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo)
.Parameters.Add("@foto", MySqlDbType.Blob).Value = rawData
.ExecuteNonQuery()
End With
conditionAtShow()
index = 0
ShowData()
Catch ex As MySqlException
MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Finally
CMDUpdate.Dispose()
CN = Nothing
End Try
End If
Else
MessageBox.Show("Input all required information", "warning", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
End If
End Sub

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


Handles btDelete.Click
Dim myConnectionString = "Database=siswa;Data Source=localhost;User Id=root;Password="
CN = New MySqlConnection(myConnectionString)
CN.Open()

Dim CMDDelete As MySqlCommand = CN.CreateCommand


If txtNIS.Text <> "" Then
If MessageBox.Show("Anda yakin menghapus data siswa dengan NIS " & txtNIS.Text & "
akan dihapus?", "Konfirmasi", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) =
Windows.Forms.DialogResult.Yes Then
Try
Dim Sql As String = "DELETE FROM siswa WHERE nis=@nis;"
With CMDDelete
.CommandText = Sql
.Parameters.Add("@nis", MySqlDbType.String, 10).Value = txtNIS.Text
.ExecuteNonQuery()
End With
conditionAtShow()
index = 0
ShowData()
Catch ex As MySqlException
MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Finally
CMDDelete.Dispose()
CN = Nothing
End Try
End If
End If
End Sub

Private Sub ShowData()


Dim myConnectionString As String = "Database=siswa;Data Source=localhost;User
Id=root;Password="
CN = New MySqlConnection(myConnectionString)
CN.Open()

Dim DataReader As MySqlDataReader = Nothing


Dim CMDShow As MySqlCommand = CN.CreateCommand()
Try
countDataInDatabase()
If countData = 0 Then
btBrowse.Enabled = False
btDelete.Enabled = False
btEdit.Enabled = False
txtNIS.Text = ""
txtNama.Text = ""
txtAlamat.Text = ""
PictureBox1.Image = Nothing
Else
indexFirst = index
indexLast = index + 1
Dim Sql As String = "SELECT * FROM siswa LIMIT " & indexFirst.ToString & " , " &
indexLast.ToString
CMDShow.CommandText = Sql
DataReader = CMDShow.ExecuteReader()

If DataReader.Read() Then
txtNIS.Text = DataReader("nis").ToString
txtNama.Text = DataReader("nama").ToString
txtAlamat.Text = DataReader("alamat").ToString
txtTanggal.Value = DataReader("tgl_lahir")
If DataReader("foto") IsNot DBNull.Value Then
Dim imageBytes() As Byte = Nothing
imageBytes = CType(DataReader("foto"), Byte())
Dim ms As New MemoryStream(imageBytes)
Dim bmap As New Bitmap(ms)
PictureBox1.Image = CType(bmap, Image)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End If
End If
End If
If countData = 1 Or countData = 0 Then
btMoFirst.Enabled = False
btMoPrev.Enabled = False
btMoNext.Enabled = False
btMoLast.Enabled = False
ElseIf index = 0 Then
btMoFirst.Enabled = False
btMoPrev.Enabled = False
btMoNext.Enabled = True
btMoLast.Enabled = True
ElseIf index = countData - 1 Then
btMoFirst.Enabled = True
btMoPrev.Enabled = True
btMoNext.Enabled = False
btMoLast.Enabled = False
Else
btMoFirst.Enabled = True
btMoPrev.Enabled = True
btMoNext.Enabled = True
btMoLast.Enabled = True
End If
Catch ex As MySqlException
MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
CMDShow.Dispose()
CN = Nothing
End Try
End Sub

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


System.EventArgs) Handles btBrowsePicture.Click
Dim dlgFile As OpenFileDialog
PictureBox1.Image = Nothing
Try
dlgFile = New OpenFileDialog
dlgFile.Filter = "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|GIF Images (*.gif)|*.gif|Bitmaps
(*.bmp)|*.bmp"
dlgFile.FilterIndex = 1
PictureBox1.Visible = True
If dlgFile.ShowDialog = Windows.Forms.DialogResult.OK Then
strImageName = dlgFile.FileName
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
PictureBox1.Image = Image.FromFile(strImageName)
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

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


Handles txtNIS.Leave
'Numerik_Valid(txtNIS.Text, txtNIS)
End Sub

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


Handles btMoFirst.Click
index = 0
ShowData()
End Sub

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


Handles btMoPrev.Click
index = index - 1
ShowData()
End Sub

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


Handles btMoNext.Click
index = index + 1
ShowData()
End Sub

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


Handles btMoLast.Click
countDataInDatabase()
index = countData - 1
ShowData()
End Sub

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


Handles btAdd.Click
txtNIS.Text = ""
txtNama.Text = ""
txtAlamat.Text = ""
txtTanggal.Text = ""
PictureBox1.Image = Nothing
status = "add"
conditionAtModify()
End Sub
Private Sub btEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btEdit.Click
PictureBox1.Image = Nothing
status = "edit"
conditionAtModify()
txtNIS.Enabled = False
txtNama.Focus()
End Sub

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


Handles btCancel.Click
conditionAtShow()
ShowData()
End Sub

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


Handles Button1.Click
Me.Close()
End Sub

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


Handles Label5.Click

End Sub

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


Handles GroupBox4.Enter

End Sub
End Class

You might also like