1

I'm having some trouble getting a loop to see more then just the first row of data. The referenced dataset function gets all the required rows with no problem therefore I'm sure the problem must be with the code.

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

If anyone could help at all or point me in the rihgt direct that would be fantastic! Thanks in advance :)

2
  • The posted answers appear correct...but why are you looping through all your user data to authenticate one user? Commented Feb 12, 2013 at 1:58
  • try never use indexes... if you change the order of your select statement, all your code will be ruined... see my comment on my answer and use the row[<string>] instead. Commented Feb 12, 2013 at 2:16

3 Answers 3

1

when you use For Each rowsLogin In dtLogin.Rows you are telling the compiler that, for each dtLogin.Rows item, assign it into the variable rowsLogin.

So, every time, inside the loop, you stop using dtLogin.Rows.Item(0).Item(2) like in If pass = dtLogin.Rows.Item(0).Item(2) Then but rather If pass = rowsLogin.Item(0).Item(2) Then

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help, but that just generates an error - "Public member 'Item' on type 'Integer' not found."
I would assume that what you're tryint to so is get the item by name, so I would use rowsLogin["my_column_name"] instead the way you are pulling out the values
0

dtLogin.Rows.Item(0).Item(1) - the (0) after Rows.Item refers to the index in the collection of rows, so you're always looking at the first row.

Instead of using dtLogin.Rows.Item(0).Item(1), etc. in your loop, use rowsLogin.Item(1).

Comments

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

For more clear code you should use rowsLogin("USER_NAME") instead of rowsLogin(1), rowsLogin("USER_PWD") instead of rowsLogin(2), etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.