Lunes, Agosto 7, 2017

Accessing Database Using Visual Basic: Log in Form and Module



------------------------------------------------------------------
Log in Code:    
---------------------------------------------------------------

Note: Types this code after " pubic class form1

--------------------------------------------
Imports System.Data.OleDb
-------------------------------------------




   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        con.Open()
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [user] WHERE [UserName] = '" & TextBox1.Text & "' AND [Password] = '" & _
                                                   TextBox2.Text & "'", con)
        Dim dr As OleDbDataReader = cmd.ExecuteReader


        ' the following variable is hold true if user is found, and false if user is not found
        Dim userFound As Boolean = False
        ' the following variables will hold the user first and last name if found.
        Dim FirstName As String = ""
        Dim LastName As String = ""
        'if found:
        While dr.Read
            userFound = True
            FirstName = dr("FirstName").ToString
            LastName = dr("LastName").ToString
        End While
        'checking the result
        If userFound = True Then
            con.Close()
            Form2.Show()
            Form2.Label11.Text = "Welcome " & FirstName & " " & LastName
        Else
            MsgBox("Sorry, username or password not found", MsgBoxStyle.OkOnly, "Invalid Login")
        End If
        con.Close()


==============================================================
CREATE A MODULE
==============================================================






Log_in Form

Walang komento:

Mag-post ng isang Komento