An application that are uses by many user, need a better security level to it. One of the security level on the application is a login screen. In this tutorial, I will try to introduce on creating a login screen to an application that are create it with visual basic.NET 2005. This codes is not a clean code and still have many bugs into it, so if anyone can create a better login screen/better code writing, please write me an e-mail at coojack@gmail.com, and I will update this post.
Let start with some basic, create a database login.mdb with table Users
Now open visual studio.net 2005 and create a new project called login. Before we do anything else, first of all move the database we just created into the folder of login project. Move the database into folder login\login\bin\debug
Ok continue, Change the properties of form1.vb :
BackColor | LightBlue |
Font+ | Microsoft Sans Serif, 8.25pt, Style=Bold |
FormBorderStyle | Fixed3D |
Text | Login |
Name | F_Login |
StartPosition | CenterScreen |
ControlBox | FALSE |
ShowInTaskbar | FALSE |
Add 2 labels, 2 textboxs and 2 buttons with properties :
Label1
BackColor | LightBlue |
Font+ | Microsoft Sans Serif, 8.25pt, style=Bold |
Text | Username |
Label2
BackColor | LightBlue |
Font+ | Microsoft Sans Serif, 8.25pt, style=Bold |
Text | Password |
Textbox1
BorderStyle | Fixed3D |
Name | txtUser |
Textbox2
BorderStyle | Fixed3D |
Name | txtPassword |
PasswordChar | * |
Button1
BackColor | LightBlue |
Font+ | Microsoft Sans Serif, 8.25pt, style=Bold |
Flatstyle | Flat |
Text | Login |
Name | btnlogin |
Button2
BackColor | LightBlue |
Font+ | Microsoft Sans Serif, 8.25pt, style=Bold |
Flatstyle | Flat |
Text | Cancel |
Name | btnbatal |
After finish changing every properties for the forms and controls, now add one more form and change the name into F_Menu.vb.
Now, before we hit into the code, first of all, lets create a module to connect between database and application. Create new module and change its name to DataConnection.vb. Write this code to DataConnection.vb :
Imports System.Data Imports
System.Data.OleDb
Namespace
AccessData Public Class DatabaseConnection Dim conect As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\Login.mdb;")
Public Function open() As OleDbConnection
conect.Open()
Return conect
End Function
Public Function close() As OleDbConnection
conect.Close()
Return conect
End Function
End Class
End Namespace
After that, create another module, and change its name to Module.vb. Write this code in it :
Module _Module
Public intResponse, msg As Integer
Public Username, Password As String
End Module
After all the module have been created, lets fill the F_login form with code :
Imports System.Data
Imports System.Data.OleDb
Public Class F_Login
#Region "Deklarasi"
Dim myConnection As New AccessData.databaseconnection
Dim objconnection As OleDbConnection
Dim objcommand As OleDbCommand
Dim objdataadapter As OleDbDataAdapter
Dim objdatareader As OleDbDataReader
Dim strsql As String
Dim objdataset As New DataSet
Dim objdatatable As New DataTable
Dim F_Menu As New F_Menu
#End Region
#Region "Sub"
Sub CekUser()
objdatatable.Clear()
strsql = ("SELECT * FROM Users WHERE username= '" & Trim(txtUser.Text) & "'")
objcommand = New OleDbCommand
objcommand.Connection = myConnection.open
objcommand.CommandType = CommandType.Text
objcommand.CommandText = strsql
objdataadapter = New OleDbDataAdapter(objcommand)
objdataadapter.Fill(objdataset, "MDT_user")
objdatatable = objdataset.Tables("MDT_user")
myConnection.close()
End Sub
Sub Find_user()
objcommand = myConnection.open.createcommand
objcommand.CommandText = ("Select username, password From users where username='" & Trim(txtUser.Text) & "'")
objdatareader = objcommand.ExecuteReader
objdatareader.Read()
Username = objdatareader.Item("username")
Password = objdatareader.Item("Password")
myConnection.close()
End Sub
Sub login()
Try
'Mencari data user berdasarkan Username yang dimasukkan pada txtuser
CekUser()
'tidak boleh mengkosongkan username & password
If txtUser.Text.Trim() = "" And txtPassword.Text.Trim() = "" Then
MsgBox("Masukan Username dan Password", MsgBoxStyle.OkOnly, "POS")
txtUser.Focus()
ElseIf txtUser.Text = "" Then
MsgBox("Masukan Username ", MsgBoxStyle.OkOnly, "POS")
txtUser.Focus()
ElseIf txtPassword.Text = "" Then
MsgBox("Masukan password ", MsgBoxStyle.OkOnly, "POS")
txtPassword.Focus()
Else
'jika username dan password tidak kosong, maka program akan mengecek
'apakah data yang dicari tersedia pada objDataTable.
'Jika Tidak (baris data = 0 ) maka akan keluar pesan
'bahwa username tidak ada
If objdatatable.Rows.Count <= 0 Then
MsgBox("Username tidak ada ", MsgBoxStyle.OkOnly, "POS")
txtUser.Focus()
Else
'jika data yang di cari ada, maka program akan mencari password
'berdasarkan username yang dimasukkan.
Find_user()
'jika password yang di masukkan salah atau tidak sama
' dengan yang ada pada tabel, maka akan keluar pesan dari program
If Password <> Trim(txtPassword.Text) Then
MsgBox("Password salah!", MsgBoxStyle.OkOnly, "POS")
txtPassword.Focus()
Exit Sub
Else
'Jika benar program akan menampilkan pada form utama
'Me.ShowInTaskbar = False
Me.Hide()
F_Menu.ShowDialog()
txtUser.Text = ""
txtPassword.Text = ""
End If
End If
End If
Catch When Err.Number <> 0
MsgBox("Tidak dapat melakukan proses" & vbCrLf & Err.Description)
MyConnection.close()
End Try
End Sub
#End Region
Private Sub btnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBatal.Click
intResponse = MessageBox.Show("Apakah anda mau keluar dari program?", Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
If intResponse = MsgBoxResult.Yes Then
End
Else
Exit Sub
End If
End Sub
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
login()
End Sub
End Class
0 comments:
Post a Comment