Problem validating multiple user
I am currently working on a project for school in which I am trying to implement a multi user login on one of my forms. I have an internal database connected in which one of my tables contains the user-names and passwords. I have a For/each loop to search the table for a matching username/password before allowing access to next form. The problem is even if the username/password is correct, my message box still appears to notify of invalid credentials, but still allows access to other form. The problem only occurs when I implement two For/each loops. With one, code works fine. Can any one see what might be wrong? Thanks
Code:
Dim h As New AdminActionForm
Dim i As New UserActionForm
For Each row As EmployeeDBDataSet1.UserName_PasswordsRow In EmployeeDBDataSet1.UserName_Passwords
If row.Password = txtLoginPassword.Text And row.UserName = txtLoginUserName.Text _
And row.Administrator = True Then
Me.Hide()
h.Show()
Exit For
ElseIf row.Password <> txtLoginPassword.Text Or row.UserName <> txtUserName.Text Then
MessageBox.Show("Invalid UserName or Password", "Incorrect Information", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit For
End If
'End If
Next
For Each row As EmployeeDBDataSet1.UserName_PasswordsRow In EmployeeDBDataSet1.UserName_Passwords
If row.Password = txtLoginPassword.Text And row.UserName = txtLoginUserName.Text _
And row.Administrator = False Then
Me.Hide()
h.Show()
Exit For
ElseIf row.Password <> txtLoginPassword.Text Or row.UserName <> txtUserName.Text Then
MessageBox.Show("Invalid UserName or Password", "Incorrect Information", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit For
End If
'End If
Next]
Re: Problem validating multiple user
Why would you use 2 for each loops here?
As far as I can tell the only difference is the status of the admin flag which would be better if you used and additional if within your existing if in the top loop.
Example:
Code:
For Each row As EmployeeDBDataSet1.UserName_PasswordsRow In EmployeeDBDataSet1.UserName_Passwords
If row.Password = txtLoginPassword.Text And row.UserName = txtLoginUserName.Text Then
If row.Administrator = True Then
Me.Hide()
h.Show()
Exit For
Else
Me.Hide()
h.Show()
Exit For
End If
ElseIf row.Password <> txtLoginPassword.Text Or row.UserName <> txtUserName.Text Then
MessageBox.Show("Invalid UserName or Password", "Incorrect Information", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit For
End If
Next
No need for the second loop
Re: Problem validating multiple user
On second look there is another issue Your code would only work correctly it the first record is a match.
You should use a flag and check it at the bottom of the loop
Code:
Dim ValidLogin as Boolean=False
For Each .....
If fields match then
ValidLogin=true
Exit For
End If
Next
If ValidLogin then
' do login stuff, check if admin or whatever
Else
'display invalid login message
End If
Note this is not the actual code but provided only to give the idea of how it could work.
Re: Problem validating multiple user
Thanks for the reply Data Miser. For the record, I originally had 1 loop, but kept running into several key problems. So I decided to "dumb it down" and work with only one loop that concentrated on the administrator only, and it worked fine. So I added the second loop to take care of non-admin user, and problem came back. Either way, your idea for the flag was the way to go!I changed the code and it works as it should now. Much appreciated :wave:
Re: Problem validating multiple user
Sorry for the strange formatting of my post and reply. I think it's from the html tags. Sorry for the inconvenience.
Re: Problem validating multiple user
Don't use html tags for you message. It makes it to hard to read.