Pada posting artikel kali ini kita akan membuat halaman login dengan level pengguna berbeda, menggunakan Visual Basic.Net dan database MySql.
Input terdiri dari user_id dan password. Pada kolom level, jika pengguna merupakan Admin, maka pengguna dapat mengelola data pengguna lain seperti menambah atau merubah data pengguna, sementara jika pengguna merupakan user maka hanya dapat membuka halaman input data buku saja.
Adapun langkah-langkahnya adalah sebagai berikut:
1. Buatlah database menggunakan MySql, nama database: DbBuku, nama tabel: Tbl_Login.
Struktur tabel dari Tbl_Login |
Isi Tbl_Login |
2. Buatlah desain untuk halaman Login:
Form Login (Frm_Login) |
3. Buatlah desain untuk halaman Kelola Data Pengguna:
Form Kelola Data Pengguna (Frm_Pengguna) |
4. Buatlah desain untuk halaman Data Buku:
Form Data Buku (Frm_Data_Buku) |
5. Tambahkan Module1 yang berisi script program dibawah ini, berfungsi untuk menghubungkan dari halaman VB.Net ke database My SQL:
Imports MySql.Data.MySqlClient
Module Module1
Public CMD As New MySqlCommand
Public DA As New MySqlDataAdapter
Public DS As New DataTable
Public conn As MySqlConnection
Public SQL As String
Sub KoneksiMySql()
Try
conn = New MySqlConnection()
conn.ConnectionString = "server=localhost;user id=root;" &
"password=;database=DbBuku"
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Catch ex As Exception
MsgBox("Tidak Dapat Terkoneksi ke Databse, Periksa Pengaturan Database My Sql")
End Try
End Sub
End Module
6. Buatlah script program untuk halaman login:
Imports MySql.Data.MySqlClient
Public Class Frm_Login
Dim gagal_login As Integer
Private Sub Frm_Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load, MyBase.Activated
Call KoneksiMySql()
Call awal()
End Sub
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
If btnOk.Text = "Data Buku" Then
Me.Hide()
Frm_Data_Buku.Show()
Else
If cek_isi() = True Then
txtUserID.Select()
Exit Sub
End If
Call periksa()
End If
End Sub
Private Function cek_isi() As Boolean
'Memastikan seluruh text terisi
If Me.txtUserID.Text.Trim = "" Or
Me.txtPassword.Text.Trim = "" Then
MsgBox("Username & Password Harus Diisi!", MsgBoxStyle.Exclamation, "Perhatian")
Call awal()
Return True
Else
Return False
End If
End Function
Sub periksa()
Dim usr, pswd As String
usr = txtUserID.Text
pswd = txtPassword.Text
Dim CMD As New MySqlCommand
Dim DR As MySqlDataReader
Call KoneksiMySql()
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
SQL = "SELECT * from tbl_login WHERE user_id = '" & txtUserID.Text & "' AND password = '" & txtPassword.Text & "'"
Try
CMD.Connection = conn
CMD.CommandText = SQL
DA.SelectCommand = CMD
DR = CMD.ExecuteReader()
DR.Read()
If DR.HasRows = True Then
If DR("level").ToString = "Admin" Then
Me.btnAkun.Visible = True
Me.btnOk.Text = "Data Buku"
Me.txtUserID.Text = ""
Me.txtPassword.Text = ""
Me.txtUserID.Enabled = False
Me.txtPassword.Enabled = False
Else
Me.Hide()
Frm_Data_Buku.Show()
End If
Else
gagal_login = gagal_login + 1
If gagal_login >= 3 Then
MsgBox("Maaf. Anda gagal_login login 3 kali." & vbCrLf &
"Aplikasi akan ditutup.")
End
End If
MsgBox("Username And Or Password Anda salah. " & vbCrLf &
"Kesempatan anda " & gagal_login & " dari 3", MsgBoxStyle.Exclamation, "Perhatian")
End If
DR.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Sub awal()
txtUserID.Text = String.Empty
txtPassword.Text = String.Empty
txtUserID.Enabled = True
txtPassword.Enabled = True
txtPassword.PasswordChar = "*"
txtPassword.MaxLength = 14
btnOk.Enabled = True
btnOk.Text = "Ok"
btnBatal.Enabled = True
btnBatal.Text = "Tutup"
btnAkun.Visible = False
txtUserID.Focus()
End Sub
Sub Form_Admin()
End Sub
Private Sub btnBatal_Click(sender As Object, e As EventArgs) Handles btnBatal.Click
If btnBatal.Text = "Batal" Then
awal()
Else
End
End If
End Sub
Private Sub txtUserID_TextChanged(sender As Object, e As EventArgs) Handles txtUserID.TextChanged, txtPassword.TextChanged
btnBatal.Text = "Batal"
End Sub
Private Sub btnAkun_Click(sender As Object, e As EventArgs) Handles btnAkun.Click
Me.Hide()
Form_Pengguna.Show()
End Sub
End Class
7. Buatlah script program untuk Kelola Data Pengguna:
Imports System.Net
Imports System.IO
Imports MySql.Data.MySqlClient
Imports System.Data
Public Class Form_Pengguna
Dim tag_simpan As String
Private Sub Form_Pengguna_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call KoneksiMySql()
tag_simpan = "baru"
Call Setup_Grid()
Call Isi_Datagrid(DataGridView1)
Call awal()
End Sub
Sub Isi_Combo_Level()
cboLevel.Items.Clear()
cboLevel.Items.Add("Admin")
cboLevel.Items.Add("User")
End Sub
Sub awal()
Isi_Combo_Level()
txtUserID.Text = String.Empty
txtPassword.Text = String.Empty
cboLevel.Text = "- Pilih -"
txtUserID.Enabled = False
txtPassword.Enabled = False
cboLevel.Enabled = False
btnInput.Enabled = True
btnSimpan.Enabled = False
btnHapus.Enabled = False
btnTutup.Enabled = True
btnTutup.Text = "Tutup"
End Sub
Sub siap_isi()
txtUserID.Enabled = True
txtPassword.Enabled = True
cboLevel.Enabled = True
btnInput.Enabled = False
btnSimpan.Enabled = True
btnHapus.Enabled = False
btnTutup.Enabled = True
btnTutup.Text = "Batal"
txtUserID.Focus()
End Sub
Sub Setup_Grid()
Call KoneksiMySql()
If conn.State = ConnectionState.Closed Then conn.Open()
SQL = "Select * from Tbl_Login order by user_id"
CMD.Connection = conn
CMD.CommandText = SQL
DA.SelectCommand = CMD
DA.Fill(DS)
With DataGridView1
.DataSource = DS
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.ReadOnly = True
.Columns(0).HeaderText = "user_id"
.Columns(1).HeaderText = "password"
.Columns(2).HeaderText = "level"
.Columns(0).Width = 20
.Columns(1).Width = 27
.Columns(2).Width = 27
End With
conn.Close()
End Sub
Sub Isi_Datagrid(grids)
Dim x As Integer
If grids.Rows.Count > 0 Then
While x < grids.Rows.Count
grids.Rows.RemoveAt(x)
End While
End If
With grids
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
.RowHeadersVisible = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End With
'============================================================
Call KoneksiMySql()
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
With CMD
.Connection = conn
.CommandType = CommandType.Text
.CommandText = "Select * from Tbl_Login"
End With
DA.SelectCommand = CMD
grids.DataSource = DS
DA.Fill(DS)
conn.Dispose()
conn.Close()
End Sub
Private Sub btnInput_Click(sender As Object, e As EventArgs) Handles btnInput.Click
Call siap_isi()
tag_simpan = "baru"
End Sub
Private Sub btnSimpan_Click(sender As Object, e As EventArgs) Handles btnSimpan.Click
Call save(tag_simpan)
Call KoneksiMySql()
Call awal()
Call Setup_Grid()
Call Isi_Datagrid(DataGridView1)
End Sub
Sub save(ByVal tag As String)
Call KoneksiMySql()
Dim CMD As New MySqlCommand
Dim SQL As String = Nothing
If tag = "baru" Then
SQL = "INSERT INTO Tbl_Login (user_id, password, level) VALUES " &
"('" & txtUserID.Text & "', '" & txtPassword.Text & "','" & cboLevel.Text & "')"
ElseIf tag = "edit" Then
SQL = "UPDATE Tbl_Login SET password='" & txtPassword.Text.ToUpper &
"',level='" & cboLevel.Text.ToUpper & "' WHERE user_id='" & txtUserID.Text & "'"
End If
Try
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
With CMD
.Connection = conn
.CommandType = CommandType.Text
.CommandText = SQL
End With
CMD.ExecuteNonQuery()
conn.Close()
Catch myerror As MySqlException
MessageBox.Show("Error: " & myerror.Message)
Finally
conn.Dispose()
End Try
End Sub
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
txtUserID.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
txtPassword.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value.ToString
tag_simpan = "edit"
txtUserID.Enabled = True
txtPassword.Enabled = True
cboLevel.Enabled = True
btnInput.Enabled = False
btnSimpan.Enabled = True
btnHapus.Enabled = True
btnTutup.Enabled = True
btnTutup.Text = "Batal"
End Sub
Private Sub btnHapus_Click(sender As Object, e As EventArgs)
End Sub
Sub delete(ByVal id As Integer)
Try
Call KoneksiMySql()
Dim SQL As String
SQL = "delete from Tbl_Login where user_id = '" & id & "'"
CMD = New MySqlCommand(SQL, conn)
CMD.ExecuteNonQuery()
MessageBox.Show("Data Berhasil Dihapus.")
Catch ex As Exception
MessageBox.Show("Data gagal_login Dihapus.")
End Try
End Sub
Private Sub btnTutup_Click(sender As Object, e As EventArgs) Handles btnTutup.Click
If btnTutup.Text = "Batal" Then
Call awal()
Else
Me.Hide()
Frm_Login.Show()
End If
End Sub
Private Sub btnHapus_Click_1(sender As Object, e As EventArgs) Handles btnHapus.Click
Dim id As Integer
id = txtUserID.Text
Call delete(id)
Call Setup_Grid()
Call Isi_Datagrid(DataGridView1)
Call awal()
End Sub
End Class
0 Response to "Halaman Login dengan Level Otoritas Pengguna Pada Visual Basic. Net dan Database MySql."
Posting Komentar