Again, please check for me this Login Form.

liziskandar

Member
Joined
Sep 21, 2005
Messages
19
Location
Malaysia
Programming Experience
Beginner
Sorry if I always troubling you people. I am in learning proses :D.

Okay, it works, I mean the database part is working. It can call the username & password to login.

But, why when I test and I purposely give correct username and wrong password, wrong username and wrong password, wrong username and correct, it will call the message box but, it still open the main form? (This time it doesn't call blank form anymore Craig!) Supposed, with wrong password it will close the form right? :confused:

VB.NET:
Imports System.Windows.Forms

Public Class frmLogin

    Inherits System.Windows.Forms.Form
    Dim iCount As Integer [COLOR=SeaGreen]' this integer is declared to help count the number of times a user tried to login.[/COLOR]

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
[COLOR=SeaGreen]'TODO: This line of code loads data into the 'GESB1DataSet.Login' table. You can move, or remove it, as needed.[/COLOR]
        Me.LoginTableAdapter.Fill(Me.GESB1DataSet.Login)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Application.Exit() [COLOR=RoyalBlue]<[COLOR=Blue]-- What is the difference between this and Me.Close/End?[/COLOR][/COLOR]
    End Sub

    Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        [COLOR=SeaGreen]'The connection string is used to describe the type of database, the security information and the location to the database.[/COLOR]
        Dim ConString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=""GESB1.mdb"";"

        '[COLOR=SeaGreen]Create a new connection object and assign the ConString Connection String above[/COLOR]
        Dim DBCon As New OleDb.OleDbConnection(ConString)

        '[COLOR=SeaGreen] g_login is a global variable defined in a Module that captures the login name and passes it from form to form.  To create this, just create a Module, say Module1.vb and in it put "Public g_login as String" {g meaning global and login to represent the global login}[/COLOR]
        g_login = Me.tbUserName.Text

        Dim Password As String = Me.tbPassword.Text

        If g_login = "" Or Password = "" Then
            MessageBox.Show("You are missing some information. Please make sure that both the username and password fields are filled out.", "Missing Info")
            Me.tbUserName.Focus()
            Return
        End If

        [COLOR=SeaGreen]' The database has two fields in the Users table.  A UserName field, which is username and declared as a text.  The other field is Password, which is a text as well.[/COLOR]
        Dim strsql As String = "SELECT [UserName], [Password] FROM Login WHERE [UserName]='" & g_login & "' "

        Dim cm As New OleDb.OleDbCommand(strsql, DBCon)
        Dim dr As OleDb.OleDbDataReader
        Dim valid As Boolean = False
        Dim HasRows As Boolean = False
        Try
            DBCon.Open()
            dr = cm.ExecuteReader
            If dr.HasRows Then
                While dr.Read
                    If Password = dr.Item("Password") Then
                        valid = True
                    End If
                End While
                HasRows = True
            End If
            dr.Close()
        Catch exO As OleDb.OleDbException
            MessageBox.Show(exO.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            If DBCon.State = ConnectionState.Open Then
                DBCon.Close()
            End If
            cm = Nothing
            dr = Nothing
            DBCon.Dispose()
            GC.Collect()
        End Try
        iCount = iCount + 1
        If valid = True Then
            Me.Hide()
            frmMain.Show()
        ElseIf iCount = 3 Then
            MessageBox.Show("Contact Your Administrator!", "Invalid Info")
            Me.Close()
        ElseIf HasRows = False Then
            MessageBox.Show("Invalid user name, try again!", "Invalid Info")
            Me.tbUserName.Focus()
            Me.tbUserName.Text = ""
            Me.tbPassword.Text = ""
        Else
            MessageBox.Show("Invalid password, try again!", "Invalid Info")
            Me.tbPassword.Focus()
            Me.tbPassword.Text = ""
        End If

        [COLOR=SeaGreen]' this line declares a variable that will point the user to the Main Screen upon a successful login.[/COLOR]
        Dim fMain As New frmMain   
        fMain.Show()
        Me.Close() [COLOR=Blue]<-- Wanted to know which is better? Close or End?[/COLOR]

    End Sub
    
End Class
Thanks in advance.

Lizzy.
 
you have to stop code from executing lower lines if login failes. try this.
VB.NET:
    If valid = True Then
            Me.Hide()
            frmMain.Show()
        ElseIf iCount = 3 Then
            MessageBox.Show("Contact Your Administrator!", "Invalid Info")
            Me.Close()
        ElseIf HasRows = False Then
            MessageBox.Show("Invalid user name, try again!", "Invalid Info")
            Me.tbUserName.Focus()
            Me.tbUserName.Text = ""
            Me.tbPassword.Text = ""
            [B]end sub[/B]
        Else
            MessageBox.Show("Invalid password, try again!", "Invalid Info")
            Me.tbPassword.Focus()
            Me.tbPassword.Text = ""
            [B]end sub[/B]
        End If
 
Back
Top