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()
If (currentUser <> username And currentPassword = password) Then
IsValid = False
ElseIf (currentUser = username And currentPassword <> password) Then
IsValid = False
ElseIf (currentUser <> username AndAlso currentPassword <> password) Then
IsValid = False
ElseIf (currentUser = username AndAlso currentPassword = password) Then
IsValid = True
usersDatasSet.Dispose()
Connection.Close()
End If
Next
If IsValid = False Then
Return False
Else
Return True
End If
usersDatasSet.Dispose()
Connection.Close()
Return False
End Function