Question Login validation error

tylersuehr7

Member
Joined
Nov 24, 2013
Messages
9
Programming Experience
1-3
I need help with this bit of coding. Everytime it loads, it loads perfects and when you enter the correct credentials it works perfect. However when you enter the wrong credentials, the login window pops back up again, but when you enter the right credentials it displays the correct message box then I get an error message, "System.IndexOutOfRangeExcepttion: There is no row at position 1. at System.Data.RBTree'1.GetNodeByIndex(Int32 userIndex) at System.Data.DataRowCollection.get_Item(Int32 index) at POS_System.MainConsole.LoginValidation() in Z:\(1) Visual Basic\POS System\POS System\GUI\MainConsole.vb:line8". I have absolutely no idea what this is or how to fix it; I have tried a bunch of different ways as well, but none of them worked. Thank you!

<MainConsole.vb> (Main Windows)
Public Class MainConsole
    Public nickname As String
    Public maxValue As Integer

    Public Sub LoginValidation()
        Try
            For i = 0 To LoginDatabaseDataSet.Tables("UserListTable").Rows.Count
                If (LoginSystem.username = LoginDatabaseDataSet.Tables("UserListTable").Rows(i).Item(0).ToString) And (LoginSystem.password = LoginDatabaseDataSet.Tables("UserListTable").Rows(i).Item(1).ToString) Then
                    nickname = LoginDatabaseDataSet.Tables("UserListTable").Rows(i).Item(2).ToString
                    MsgBox("You are now logged in as: " + nickname)
                    MainMenu_LoggednBox.Text = nickname
                    Return
                Else
                    MsgBox("Username or password invalid.", MsgBoxStyle.Exclamation, "Logon Message")
                    LoginSystem.UsernameBox.Clear()
                    LoginSystem.PasswordBox.Clear()
                    If (LoginSystem.ShowDialog = Windows.Forms.DialogResult.OK) Then
                        LoginValidation()
                        LoginSystem.Dispose()
                    Else
                        End
                        Me.Dispose()
                    End If
                End If
            Next

        Catch error_login As Exception
            MsgBox(error_login.ToString)
            End
        End Try
    End Sub
    Private Sub MainConsole_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'ItemsDatabaseDataSet.ItemListTable' table. You can move, or remove it, as needed.
        Me.ItemListTableTableAdapter.Fill(Me.ItemsDatabaseDataSet.ItemListTable)
        'TODO: This line of code loads data into the 'LoginDatabaseDataSet.UserListTable' table. You can move, or remove it, as needed.
        Me.UserListTableTableAdapter.Fill(Me.LoginDatabaseDataSet.UserListTable)
        maxValue = ItemsDatabaseDataSet.Tables("ItemListTable").Rows.Count


        If (LoginSystem.ShowDialog = Windows.Forms.DialogResult.OK) Then
            LoginValidation()
            LoginSystem.Dispose()
        Else
            End
            Me.Dispose()
        End If
    End Sub

    Private Sub BarcodeBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BarcodeBox.TextChanged
        Dim rowIndex As Integer
        rowIndex = BarcodeBox.Text
        For i = 0 To ItemsDatabaseDataSet.Tables("ItemListTable").Rows.Count
            If (rowIndex = ItemsDatabaseDataSet.Tables("ItemListTable").Rows.Count) Then
                ItemNameBox.Text = ItemsDatabaseDataSet.Tables("ItemListTable").Rows(rowIndex).Item(1).ToString
                ItemPriceBox.Text = ItemsDatabaseDataSet.Tables("ItemListTable").Rows(rowIndex).Item(2)
            End If
        Next
    End Sub

    Private Sub SearchItemButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
End Class

<LoginSystem.vb>(Login Window)
Imports System.Windows.Forms

Public Class LoginSystem
    Public username, password As String

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        DialogResult = Windows.Forms.DialogResult.Abort
        Me.Close()
    End Sub

    Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginButton.Click
        If (UsernameBox.Text.Trim.Length = 0) Or (PasswordBox.Text.Trim.Length = 0) Then
            MsgBox("Either username or password was left blank! Please enter both username and password or contact an administrator.", MsgBoxStyle.Exclamation, "Logon Message")
            Return
        End If
        If (UsernameBox.Text = "Exit") And (PasswordBox.Text = "Exit") Then
            DialogResult = Windows.Forms.DialogResult.Abort
            Me.Close()
        End If

        username = UsernameBox.Text
        password = PasswordBox.Text
        DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
End Class
 
Last edited by a moderator:
Firstly, I have formatted your code snippets to make them more readable. Please do so for us in future.

As for the issue, that error message is saying that you are trying to get the second row from a DataTable that contains fewer than two rows.
 
Thanks for responding, and I shall do so in future questions. I understand that now but how could I amend the code for that error to go away? I tried rearranging the If statements and For statements but none seem to be in my favor and I am clueless on how to fix this error. Again thanks for your response.
 
Well, first of all, what EXACTLY are you trying to achieve? Perhaps it would be worth your while checking this out:

WinForms Login

Make sure to follow the link in step 5.
 
Hey thanks again, that helps a lot. I was trying to make a login that connects to a database (microsoft access) to get user names and passwords. And I think that your code for the login will work with what i'm trying to do, i'll test it when I get to school and i'll reply to let you know if it worked or not. Thanks
 
Back
Top