06 May 2015

Artikel ini lanjutan dari part 1.

Dan ini dia source code langkap nya:

Imports System.Data
Imports MySql.Data.MySqlClient

Public Class LoginForm1
    Dim iFail As Integer

    Private Sub LoginForm1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
        iFail = 0
    End Sub

    Function CheckLogin(ByVal UserName As String, _
        ByVal Password As String) As Integer

       
Dim conn As MySqlConnection
       
Dim cmd As New MySqlCommand
       
Dim objValue As Object

        conn =
New MySqlConnection()
        conn.ConnectionString = "server=localhost;user id=root;" & _
                                "password=;database=latihan"
        Try
            conn.Open()
            cmd.Connection = conn
            cmd.CommandText = "SELECT COUNT(username) AS getin " & _
                              "FROM tbluser WHERE username = " & _
                              "'" & UserName & "' AND " & _
                              "password = '" & Password & "'"

            objValue = cmd.ExecuteScalar()
            conn.Close()
            If objValue Is Nothing Then
                Return 0
            Else
                Return
objValue.ToString
            End If
        Catch
myerror As MySqlException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
            conn.Dispose()
        End Try
        Return
0
    End Function

    Private Sub OK_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles OK.Click
        Dim strUserName As String = UsernameTextBox.Text
       
Dim strPassword As String = PasswordTextBox.Text

        If strUserName = String.Empty Then _
           MsgBox("Username belum diisi!") : Exit Sub
       
If strPassword = String.Empty Then _
           MsgBox("Password belum diisi!") : Exit Sub

        Try
            If
CheckLogin(strUserName, strPassword) > 0 Then
                MsgBox("Welcome " & strUserName & "!")
                'tulis code lainnya setelah login disini
                Me.Close()
            Else
                iFail = iFail + 1
                If iFail >= 3 Then
                    MsgBox("Gagal login 3 kali." & vbCrLf & _
                           "Aplikasi akan ditutup.")

                    End
                End If
                MsgBox("Username/password Salah." & vbCrLf & _
                       "Silahkan cek lagi!")
            End If
        Catch
ex As Exception
            MsgBox("Error Login: " & ex.Message)
        End Try
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Cancel.Click

        Me.Close()

    End Sub

End Class


Lalu-lalu mari kita coba run login form nya:

Saat kita klik OK sebelum mengisi Username akan muncul pesan:
Saat kita klik OK sebelum mengisi Password akan muncul pesan:
 Jika kita salah mengiputkan username atau password maka muncul pesan:
Jika pengisian username dan password benar maka login akan sukses.
Jika kita melakukan kesalahan login sebanyak 3 kali maka aplikasi akan ditutup.



Click here if you like this article.


0 comments: