VB中的登录系统问题 [英] Issue with login system in VB
问题描述
大家好,
i我的登录系统有问题,并寻求一些极客帮助:)
使用第一个用户时(管理员)系统做了它需要做的事情。
但是当我尝试使用其他用户登录时它不会工作。
我收到我的错误用户名和密码未知
当我从代码中删除以下行时,我可以与所有其他用户一起登录
ElseIf(currentUser<>用户名AndAlso currentPassword<>密码)然后
MessageBox.Show(用户名和密码未知,错误,MessageBoxButtons.OK,MessageBoxIcon.Error )
返回错误
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
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
推荐答案
您好b $ b
首先您需要移动
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
谢谢你的回复。我会尝试这个
Thanks for your reply. I will try this
这篇关于VB中的登录系统问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!