2013-02-12 93 views
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 

如果任何人都可以幫助我,或者直接指向我的rihgt直接,那太棒了!在此先感謝:)

+0

張貼的答案看起來是正確的......但爲什麼你循環所有的用戶數據來驗證一個用戶? – 2013-02-12 01:58:05

+0

嘗試從不使用索引...如果您更改了選擇語句的順序,您的所有代碼都將被破壞...請參閱我對我的回答的評論,並使用行[]'代替。 – balexandre 2013-02-12 02:16:08

回答

0

dtLogin.Rows.Item(0).Item(1) - 在(0)Rows.Item指索引行的集合中後,讓你」總是在看第一排。

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

+0

這就像一個魅力工作 - 謝謝:) – 2013-02-12 02:09:44

1

當您使用For Each rowsLogin In dtLogin.Rows您告訴編譯器,對於每個dtLogin.Rows項目,將其分配到變量rowsLogin

所以,每一次,在循環中,你停止使用dtLogin.Rows.Item(0).Item(2)If pass = dtLogin.Rows.Item(0).Item(2) Then而是If pass = rowsLogin.Item(0).Item(2) Then

+0

感謝您的幫助,但這只是產生一個錯誤 - 「未找到類型爲」Integer「的公共成員'Item'。」 – 2013-02-12 02:07:27

+0

我會假設你正在嘗試這樣做是通過名稱獲得項目,所以我會使用'rowsLogin [「my_column_name」]'而不是你拉出值的方式 – balexandre 2013-02-12 02:10:04

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)等