Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB VB.NET
Hi all,
 
i have an issue with my loginsystem and seek for some geek help Smile | :)
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

 

 
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 22-Apr-13 13:23pm
Comments
Sergey Alexandrovich Kryukov at 22-Apr-13 19:00pm
   
So, VB or VB.NET?
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi
 
First you need to move the
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:
 
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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Thanks for your reply. I will try this
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100