When I try debugging this, it comes up with an error on the line "If txtUsername.Text = ds.Tables("AddressBook").Rows(inc).Item(1) Then". It says NullReferenceException was unhandled. Object reference not set to an instance of an object.

Public Class MainLogin
Dim MaxRows As Integer
Dim inc As Integer = 0
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim dbPassword As String
Dim sql As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim LoginCorrect As Boolean = False
Dim NoRecordsLeft As Boolean = False

 Private Sub MainLogin_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    dbProvider = "provider=Microsoft.ACE.OLEDB.12.0"
    dbSource = "Data Source = G:\Sixth Form\Computing\Year 13\COMP4 - Project\Program Draft\DatabaseTest.accdb;" 'check drive letter before debugging
    dbPassword = "Jet OLEDB:Database Password=database;"
    con.ConnectionString = dbProvider & dbSource & dbPassword


    sql = "SELECT * FROM tblPassword"
    da = New OleDb.OleDbDataAdapter(Sql, con)
    da.Fill(ds, "AddressBook")
End Sub

Private Sub btnLogin_Click(sender As System.Object, e As System.EventArgs) Handles btnLogin.Click
        If txtUsername.Text = ds.Tables("AddressBook").Rows(inc).Item(1) Then
            If txtPassword.Text = ds.Tables("AddressBook").Rows(inc).Item(2) Then
                LoginCorrect = True
                LoginCorrect = False
            End If
            LoginCorrect = False
        End If
        inc = inc + 1
        If inc = ds.Tables("AddressBook").Rows.Count Then
            NoRecordsLeft = True
        End If
    Loop Until LoginCorrect = True Or NoRecordsLeft = True
    If LoginCorrect = True Then
        MessageBox.Show("You have succesfully logged in correctly!!", "WOOOOOOOOOOOOO!")
    End If
    If NoRecordsLeft = True Then
        MessageBox.Show("Wrong username or password, please try again!!")
    End If
End Sub

Private Sub btnChangePassword_Click(sender As System.Object, e As System.EventArgs) Handles btnChangePassword.Click
End Sub

End Class

Try handling it with an inline if statment:

If txtUsername.Text = IIF(IsDBNull(ds.Tables("AddressBook").Rows(inc).Item(1)),"",ds.Tables("AddressBook").Rows(inc).Item(1)) Then
   'Do Work
End IF

It is possible that you are returning a null entry from the database, thus throwing a NRE.

By using this inline if, you return "" for null (not throwing a NRE) and can then debug it with:


Edited 4 Years Ago by Begginnerdev

This article has been dead for over six months. Start a new discussion instead.