Click here to Skip to main content
15,895,772 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

i have an issue with my loginsystem and seek for some geek help :)
When using the first made user (admin) the system does what it needs to do.
But when i try to login with a different user it wont work.
And i get my error "username and password unknown"

When i remove following lines from the code i can login with all other users

ElseIf (currentUser <> username AndAlso currentPassword <> password) Then
MessageBox.Show("Username and password unknown", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False




VB
Public Function Login(ByVal username As String, ByVal password As String)
        Dim usersDatasSet As New DataSet()
        usersDataAdapter.FillSchema(usersDatasSet, SchemaType.Source, "Users")
        usersDataAdapter.Fill(usersDatasSet, "Users")
        Dim table As DataTable = usersDatasSet.Tables("Users")

        For i As Integer = 0 To table.Rows.Count - 1
            Dim currentUser As String = table.Rows(i)("Username").ToString().Trim()
            Dim currentPassword As String = table.Rows(i)("Password").ToString().Trim()


            'Check input

            If (currentUser <> username And currentPassword = password) Then
                MessageBox.Show("Unknown user", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False

            ElseIf (currentUser = username And currentPassword <> password) Then
                MessageBox.Show("Wrong password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False


            ElseIf (currentUser <> username AndAlso currentPassword <> password) Then
                MessageBox.Show("Username and  password unknown", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False

            ElseIf (currentUser = username AndAlso currentPassword = password) Then
                usersDatasSet.Dispose()
                Connection.Close()
                Return True
            End If

        Next
        usersDatasSet.Dispose()
        Connection.Close()
        Return False
    End Function


Thanks for any help in this issue
Posted
Comments
Sergey Alexandrovich Kryukov 22-Apr-13 19:00pm    
So, VB or VB.NET?
—SA

Hi
First you need to move the
VB
Return
statements out of the loop as condition check may fail when checking even the first row. But the correct user name and password combination may be present in the second row. Use flags to set the value to be returned and check that flag once the loop is over.

Here is the sample changed code:

VB
Public Function Login(ByVal username As String, ByVal password As String)
        Dim usersDatasSet As New DataSet()
        usersDataAdapter.FillSchema(usersDatasSet, SchemaType.Source, "Users")
        usersDataAdapter.Fill(usersDatasSet, "Users")
        Dim table As DataTable = usersDatasSet.Tables("Users")

        Dim currentUser As String = String.Empty
        Dim currentPassword As String = String.Empty
        Dim IsValid As Boolean = False

        For i As Integer = 0 To table.Rows.Count - 1
            currentUser = table.Rows(i)("Username").ToString().Trim()
            currentPassword = table.Rows(i)("Password").ToString().Trim()

            'Check input
            If (currentUser <> username And currentPassword = password) Then
                IsValid = False
                ' MessageBox.Show("Unknown user", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                'Return False

            ElseIf (currentUser = username And currentPassword <> password) Then
                IsValid = False
                'MessageBox.Show("Wrong password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                'Return False
            ElseIf (currentUser <> username AndAlso currentPassword <> password) Then
                IsValid = False
                'MessageBox.Show("Username and  password unknown", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                'Return False
            ElseIf (currentUser = username AndAlso currentPassword = password) Then
                IsValid = True
                usersDatasSet.Dispose()
                Connection.Close()
                'Return True
            End If
        Next

        ' Check if Validation succeeded
        If IsValid = False Then
            'Show Message
            Return False
        Else
            Return True
        End If
        usersDatasSet.Dispose()
        Connection.Close()
        Return False
    End Function
 
Share this answer
 
v2
Thanks for your reply. I will try this
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900