Header Ads Widget

How to make Register and login form in VB.net and MS Access database.


Step 1 => Create Database

Database name "test.accdb" and table name "login"



Step 2 =>Connect to MS-Access Database

Following Code


Imports System.Data.OleDb
Imports System.IO


Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\test.accdb")

Step 3 =>Browse Img to PictureBox

Following Code


    Private Sub Btn_browse_Click(sender As Object, e As EventArgs) Handles Btn_browse.Click
        Dim pop As OpenFileDialog = New OpenFileDialog
        If pop.ShowDialog  Windows.Forms.DialogResult.Cancel Then
            register_picturebox.Image = Image.FromFile(pop.FileName)
        End If
    End Sub
Step 4 =>Register User Details in Access Database

Following Code


            Try
                conn.Open()
                Dim cmd As New OleDb.OleDbCommand("insert into login(`firstname`,`lastname`,`username`,`password`,`dob`,`role`,`status`,`pic`) values (@firstname,@lastname,@username,@password,@dob,@role,@status,@pic)", conn)
                Dim i As New Integer
                cmd.Parameters.Clear()
                cmd.Parameters.AddWithValue("@firstname", txt_firstname.Text)
                cmd.Parameters.AddWithValue("@lastname", txt_lastname.Text)
                cmd.Parameters.AddWithValue("@username", txt_username.Text)
                cmd.Parameters.AddWithValue("@password", txt_password.Text)
                cmd.Parameters.AddWithValue("@dob", CDate(dob_picker.Value))
                cmd.Parameters.AddWithValue("@role", Combo_Role.Text)
                cmd.Parameters.AddWithValue("@status", CBool(Checkbox_status.Checked.ToString))

            'image convert to binary format

                Dim FileSize As New UInt32
                Dim mstream As New System.IO.MemoryStream
                register_picturebox.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
                Dim picture() As Byte = mstream.GetBuffer
                FileSize = mstream.Length
                mstream.Close()
                cmd.Parameters.AddWithValue("@pic", picture)
                i = cmd.ExecuteNonQuery
                If i > 0 Then
                    MsgBox("New User Register Success !", vbInformation)
                Else
                    MsgBox("New User Register Failed !", vbCritical)
                End If
                txt_firstname.Clear()
                txt_lastname.Clear()
                txt_username.Clear()
                txt_password.Clear()
                Combo_Role.Text = ""
                register_picturebox.Image = Nothing
                Checkbox_status.Checked = False
                Me.Dispose()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            conn.Close()
            login.usercomboload()

Step 5 =>Username Load by Combobox

Following Code


Sub usercomboload()
        conn.Open()
        combo_username.Items.Clear()
        Dim cmd As New OleDb.OleDbCommand("select * from login", conn)
        dr = cmd.ExecuteReader
        While dr.Read
            combo_username.Items.Add(dr.GetString(3))
        End While
        dr.Close()

        conn.Close()
    End Sub

Step 5 =>Username Change by Picturebox Image Loading

Following Code


    Private Sub combo_username_SelectedIndexChanged(sender As Object, e As EventArgs) Handles combo_username.SelectedIndexChanged
        Dim picture() As Byte
        user_picturebox.Image = Nothing

        Try
            conn.Open()
            Dim cmd As New OleDb.OleDbCommand("Select * from login where username=@username", conn)
            cmd.Parameters.Clear()
            cmd.Parameters.AddWithValue("@username", combo_username.Text)
            da = New OleDbDataAdapter
            dt = New DataTable
            da.SelectCommand = cmd
            da.Fill(dt)

            picture = dt.Rows(0).Item("pic")
            Dim mstream As New System.IO.MemoryStream(picture)
            user_picturebox.Image = Image.FromStream(mstream)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        conn.Close()
    End Sub

Step 6 =>Login into Access

Following Code


    Sub login()
        Try
            conn.Open()
            Dim cmd As New OleDb.OleDbCommand("select*from login where username=@username AND password=@password", conn)
            cmd.Parameters.Clear()
            cmd.Parameters.AddWithValue("@username", combo_username.Text)
            cmd.Parameters.AddWithValue("@password", txt_password.Text)
            Dim username As String = ""
            Dim password As String = ""
            dr = cmd.ExecuteReader
            If (dr.Read() = True) Then
                username = dr("username")
                password = dr("password")
                combo_username.Text = ""
                txt_password.Clear()

                Form2.ShowDialog()
            Else
                MsgBox("Worng Password !", vbCritical)
                txt_password.Clear()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        conn.Close()
    End Sub

Step 7 =>Password Show or Hide

Following Code


    Private Sub checkbox_Showpass_CheckedChanged(sender As Object, e As EventArgs) Handles checkbox_Showpass.CheckedChanged
        If checkbox_Showpass.Checked = True Then
            txt_password.UseSystemPasswordChar = False
        Else
            txt_password.UseSystemPasswordChar = True
        End If
    End Sub
Step 8 =>Final Form Design

Following Code


Sub loadpic()
        Dim picture() As Byte
        Try
            conn.Open()
            Dim cmd As New OleDb.OleDbCommand("Select * from login where username=@username", conn)
            cmd.Parameters.Clear()
            cmd.Parameters.AddWithValue("@username", combo_username.Text)
            da = New OleDbDataAdapter
            dt = New DataTable
            da.SelectCommand = cmd
            da.Fill(dt)
            With Form2
                picture = dt.Rows(0).Item("pic")
                .Label1.Text = dt.Rows(0).Item("firstname") & " " & dt.Rows(0).Item("lastname")
                .Label2.Text = dt.Rows(0).Item("role")
                Dim mstream As New System.IO.MemoryStream(picture)
                .Main_Picturebox.Image = Image.FromStream(mstream)
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        conn.Close()
    End Sub

Thanks

Happy Coding.....

Subscribe For More Projects...

Post a Comment

0 Comments