1

我在获取循环以查看更多数据时遇到了一些麻烦,而不仅仅是第一行数据。引用的数据集函数可以毫无问题地获取所有必需的行,因此我确定问题一定出在代码上。

Dim dtLogin As System.Data.DataTable
    Dim userDetails As New dsMembersTableAdapters.mi_membersTableAdapter
    Dim rowsLogin As System.Data.DataRow

    'Fill datatable using method from dataset
    dtLogin = userDetails.GetUserData()

    'Find cotrols hidden in Login View
    Dim user As String = txtUser.Text
    Dim pass As String = txtPass.Text

    'Search all users
    For Each rowsLogin In dtLogin.Rows
        'Find Username Entered
        If user = dtLogin.Rows.Item(0).Item(1) Then
            'Checks users password matches
            If pass = dtLogin.Rows.Item(0).Item(2) Then
                If dtLogin.Rows.Item(0).Item(6) = 1 Then
                    'Log User In
                    FormsAuthentication.RedirectFromLoginPage(dtLogin.Rows.Item(0).Item(1), True)
                Else
                    'Account Not Active Message
                    lblValidation.Text = "There is a problem with your account, please contact the website administration"
                End If
            Else
                'Incorrect Password Message
                lblValidation.Text = "Incorrect Password"
            End If
        Else
            'No User in DB Message
            lblValidation.Text = "No User Found" + dtLogin.Rows.Item(0).Item(1)
        End If
    Next

如果有人可以提供帮助或直接指向我,那就太好了!提前致谢 :)

4

3 回答 3

1

当您使用时,For Each rowsLogin In dtLogin.Rows您是在告诉编译器,对于每个dtLogin.Rows项目,将其分配给变量rowsLogin.

所以,每次,在循环内,你停止使用dtLogin.Rows.Item(0).Item(2)like inIf pass = dtLogin.Rows.Item(0).Item(2) Then而是If pass = rowsLogin.Item(0).Item(2) Then

于 2013-02-12T01:56:44.910 回答
0
dim bUserFound as boolean = false       
For Each rowsLogin In dtLogin.Rows
            'Find Username Entered
            If user = rowsLogin(1) Then
bUserFound = true
                'Checks users password matches
                If pass = rowsLogin(2) Then
                    If rowsLogin(6) = 1 Then
                        'Log User In
                        FormsAuthentication.RedirectFromLoginPage(rowsLogin(1), True)
                    Else
                        'Account Not Active Message
                        lblValidation.Text = "There is a problem with your account, please contact the website administration"
                    End If
                Else
                    'Incorrect Password Message
                    lblValidation.Text = "Incorrect Password"
                End If
            Else
                'No User in DB Message
               ' lblValidation.Text = "No User Found" + rowsLogin(1)

            End If
        Next 

if not bUserFound then
lblValidation.Text = "No User Found"
end if

要获得更清晰的代码,您应该使用 rowsLogin("USER_NAME") 而不是 rowsLogin(1)、rowsLogin("USER_PWD") 而不是 rowsLogin(2) 等。

于 2013-02-12T10:49:32.843 回答
0

dtLogin.Rows.Item(0).Item(1)- Rows.Item 之后的 (0) 指的是行集合中的索引,因此您始终查看第一行。

而不是dtLogin.Rows.Item(0).Item(1)在循环中使用等,使用rowsLogin.Item(1).

于 2013-02-12T01:57:23.263 回答