Cara Membuat Form Ganti Password Vb.net

Form rubah password ini biasanya digunakan oleh user untuk menganti password log in nya, Sebenarnya hal itu bisa dilakukan pada form tambah user, namun Form tambah user tersebut hanya  bisa diakses oleh Supervisor atau manajer,
Untuk itu form ganti password ini sangat bermanfaat bagi user yang inginmengganti password login nya tanpa harus bilang2 dulu kepada Supervisor atau Manajernya,,,,

Berikut Bentuk Form nya :




untuk kode nya sendiri saya menggunakan KOnsep OOP.

1. Buat Modul dan isikan kode berikut 
Imports System
Imports System.Data
Imports MySql.Data.MySqlClient

Module mdlMain
    Public strCon As String = "Server=localhost;User id=root;Database=db_dedi"
    '"Provider=Microsoft.Jet.sql.4.0;Data Source=\Parkir\SisPeTang\Db_Piutang.mdb"
    Public blnReturn As Boolean
    Public strSql As String
    Public mycon As MySqlConnection
    Public objReader As MySqlDataReader
    Public objAdapter As MySqlDataAdapter
    Public objDataTable As DataTable
    Public objCommand As MySqlCommand
    Public objDataSet As DataSet
    Public objDataRow As DataRow


    ' membuat warna zebra pada datagridview
    Public Sub djieChangeColorGrid(ByVal pDataGridView As DataGridView)
        For Each iRow As DataGridViewRow In pDataGridView.Rows
            For Each iCell As DataGridViewCell In iRow.Cells
                If iRow.Index Mod 2 = 0 Then
                    iCell.Style.BackColor = Color.AliceBlue
                Else
                    iCell.Style.BackColor = Color.LightBlue
                End If
            Next
        Next
    End Sub

End Module
 
2. Buat classUser untuk mengakses database

Imports System
Imports System.Data
Imports MySql.Data.MySqlClient

Public Class clsUser

    Private blnReturn As Boolean
    Private strUser_id As String
    Private strUser_name As String
    Private strPass As String

    Public Property User_id()
        Get
            Return strUser_id
        End Get
        Set(ByVal value)
            strUser_id = value
        End Set
    End Property

    Public Property User_name()
        Get
            Return strUser_name
        End Get
        Set(ByVal value)
            strUser_name = value
        End Set
    End Property

    Public Property pass()
        Get
            Return strPass
        End Get
        Set(ByVal value)
            strPass = value
        End Set
    End Property
 
    Public Function getLogin() As Boolean
        Try
            mycon = New MySqlConnection(strCon)
            mycon.Open()
            strSql = "select user_id, pass from tb_user where user_id = '" & User_id & "' and pass = '" & pass & "'"
            objCommand = New MySqlCommand(strSql, mycon)
            objReader = objCommand.ExecuteReader(CommandBehavior.Default)
            If objReader.HasRows Then
                blnReturn = True
            Else
                blnReturn = False
            End If
        Catch ex As Exception
            blnReturn = False
            MsgBox(ex.Message.ToString)
        Finally
            mycon.Close()
            objCommand.Dispose()
            mycon = Nothing
            objCommand = Nothing
        End Try
        Return blnReturn
    End Function

    Public Function isUbahPassword() As Boolean
        Try
            mycon = New MySqlConnection(strCon)
            mycon.Open()
            strSql = "Update tb_user set pass ='" & pass & "' where user_id ='" & User_id & "'"
            objCommand = New MySqlCommand(strSql, mycon)
            If objCommand.ExecuteNonQuery Then
                blnReturn = True
            Else
                blnReturn = False
            End If
        Catch ex As Exception
            blnReturn = False
            objCommand.Dispose()
            objCommand = Nothing
            mycon = Nothing
        End Try
        Return blnReturn
    End Function
 
3. Double klik pda form nya

Imports MySql.Data.MySqlClient

Public Class frmRubahPass


    Private objUser As clsUser

    Private Sub kosong()
        txtPass.Text = ""
        txtPassbaru1.Text = ""
        txtPassBaru2.Text = ""
        txtUserName.Text = ""
    End Sub

    Private Sub txtPassBaru2_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPassBaru2.LostFocus
        If txtPassbaru1.Text <> txtPassBaru2.Text Then
            MsgBox("Password tidak sama")
        End If
    End Sub

    Private Sub cekUserIdPass()
        objUser = New clsUser
        With objUser
            .User_id = txtUserName.Text
            .pass = txtPass.Text
            If .getLogin Then
                txtPassbaru1.ReadOnly = False
                txtPassBaru2.ReadOnly = False
            Else
                MsgBox("User Id lama dan Password lama Tidak sesuai")
                txtPass.Text = ""
                txtUserName.Text = ""
                txtUserName.Focus()
            End If
        End With
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        objUser = New clsUser
        With objUser
            .User_id = txtUserName.Text
            .pass = txtPassBaru2.Text
            If .isUbahPassword() Then
                MsgBox("Password Berhasil Dirubah")
            Else
                MsgBox("Gagal merubah Password")
            End If
        End With
    End Sub

    Private Sub txtPass_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPass.LostFocus
        Call cekUserIdPass()
    End Sub
'saat form d jalankan
    Private Sub frmRubahPass_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call kosong()
        txtPassbaru1.ReadOnly = True
        txtPassBaru2.ReadOnly = True
    End Sub


Sekian postingan Kali ini
semoga bermanfaat,,,,,,

Comments

Post a Comment

Popular Posts