1,105,312 Community Members

User Login Codes using VB and Access..

Member Avatar
tomexlfc
Light Poster
29 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 2 [?]
 
0
 

Hello,

I wrote some codes for student login. the codes checks for the "username", "password" as well as their type of "status", from the database. once this input are met, the student logs in successful, otherwise a message pops up.

The problem i encountered is this, i have 11 student records in my database and only the first 6 students can login successfully. the remaining 5 students cannot login. Despite entering the correct username, password and status for the last 5 students in the database, the message "Invalid username and password" comes up. I don't know where my code went wrong or maybe i didn't write the right codes to accept all student records as well as new ones.

Here's my code for the login:

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

        If txtUsername.Text = "" Then
            MessageBox.Show("Please enter your Student Id to login", "South City College", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtUsername.Focus()
        ElseIf txtPassword.Text = "" Then
            MessageBox.Show("Please enter your Password to login", "South City College", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtPassword.Focus()
        ElseIf cboStatus.Text = "" Then
            MessageBox.Show("Please select your status to login", "South City College", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Else

            Dim conLogin As New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.Oledb.12.0; Data Source = D:\DB2Test.accdb")
            Dim commLogin As New OleDb.OleDbCommand("SELECT Student.Student_ID, Student.Password, Student.Status FROM Student WHERE Student_ID = @Username AND Password = @password AND Status = @status", conLogin)

            Dim usernameParam As New OleDb.OleDbParameter("@username", Me.txtUsername.Text)
            Dim passwordParam As New OleDb.OleDbParameter("@password", Me.txtPassword.Text)
            Dim statusParam As New OleDb.OleDbParameter("@status", Me.cboStatus.SelectedItem)

            commLogin.Parameters.Add(usernameParam)
            commLogin.Parameters.Add(passwordParam)
            commLogin.Parameters.Add(statusParam)

            commLogin.Connection.Open()

            Dim reader As OleDb.OleDbDataReader = commLogin.ExecuteReader()

            If reader.HasRows Then
                MessageBox.Show("Login successful!", "South City College", MessageBoxButtons.OK, MessageBoxIcon.Information)
                frmDB2Test.Show()
                Me.Hide()

            Else
                MessageBox.Show("Invalid Student Data! Please enter correct Student Data.", "South City College", MessageBoxButtons.OK, MessageBoxIcon.Error)
                txtUsername.Clear()
                txtPassword.Clear()
                cboStatus.Text = ""
                txtUsername.Focus()

            End If

            commLogin.Connection.Close()

        End If

    End Sub
Member Avatar
ryanjayson
Junior Poster in Training
72 posts since Jan 2012
Reputation Points: -4 [?]
Q&As Helped to Solve: 16 [?]
Skill Endorsements: 1 [?]
 
0
 

Hello,

I wrote some codes for student login. the codes checks for the "username", "password" as well as their type of "status", from the database. once this input are met, the student logs in successful, otherwise a message pops up.

The problem i encountered is this, i have 11 student records in my database and only the first 6 students can login successfully. the remaining 5 students cannot login. Despite entering the correct username, password and status for the last 5 students in the database, the message "Invalid username and password" comes up. I don't know where my code went wrong or maybe i didn't write the right codes to accept all student records as well as new ones.

Here's my code for the login:

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

        If txtUsername.Text = "" Then
            MessageBox.Show("Please enter your Student Id to login", "South City College", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtUsername.Focus()
        ElseIf txtPassword.Text = "" Then
            MessageBox.Show("Please enter your Password to login", "South City College", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtPassword.Focus()
        ElseIf cboStatus.Text = "" Then
            MessageBox.Show("Please select your status to login", "South City College", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Else

            Dim conLogin As New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.Oledb.12.0; Data Source = D:\DB2Test.accdb")
            Dim commLogin As New OleDb.OleDbCommand("SELECT Student.Student_ID, Student.Password, Student.Status FROM Student WHERE Student_ID = @Username AND Password = @password AND Status = @status", conLogin)

            Dim usernameParam As New OleDb.OleDbParameter("@username", Me.txtUsername.Text)
            Dim passwordParam As New OleDb.OleDbParameter("@password", Me.txtPassword.Text)
            Dim statusParam As New OleDb.OleDbParameter("@status", Me.cboStatus.SelectedItem)

            commLogin.Parameters.Add(usernameParam)
            commLogin.Parameters.Add(passwordParam)
            commLogin.Parameters.Add(statusParam)

            commLogin.Connection.Open()

            Dim reader As OleDb.OleDbDataReader = commLogin.ExecuteReader()

            If reader.HasRows Then
                MessageBox.Show("Login successful!", "South City College", MessageBoxButtons.OK, MessageBoxIcon.Information)
                frmDB2Test.Show()
                Me.Hide()

            Else
                MessageBox.Show("Invalid Student Data! Please enter correct Student Data.", "South City College", MessageBoxButtons.OK, MessageBoxIcon.Error)
                txtUsername.Clear()
                txtPassword.Clear()
                cboStatus.Text = ""
                txtUsername.Focus()

            End If

            commLogin.Connection.Close()

        End If

    End Sub

Try to retrieve first in you database access using the SQL to check wheres the problem and also check your field data type.

SELECT Student.Student_ID, Student.Password, Student.Status FROM Student WHERE Student_ID ='Username' AND Password ='password' AND Status ='status'
Member Avatar
tomexlfc
Light Poster
29 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 2 [?]
 
0
 

Try to retrieve first in you database access using the SQL to check wheres the problem and also check your field data type.

SELECT Student.Student_ID, Student.Password, Student.Status FROM Student WHERE Student_ID ='Username' AND Password ='password' AND Status ='status'

Hi Ryan, this one seems not to find anything entered in the username, password and status textbox. It keeps giving the message "Invalid Username and Password" anytime I enter correct login details.

The "@" symbols seems to work for it, but still it sees only some records and the rest not working. I meant this one:

"SELECT Student.Student_ID, Student.Password, Student.Status FROM Student WHERE Student_ID = @Username AND Password = @password AND Status = @status"
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: