I keep getting an error when trying to check values from a database and read from a database to text boxes. At the for i as integer
line I get the following error message:
An unhandled exception of type 'System.NullReferenceException' occurred in VBPROJECT.exe despite the code working earlier.
Here's the code:
Imports System.Data.OleDb
Public Class StudentLogin
Dim objConnection As New OleDb.OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Customers.accdb")
Dim objDA As _
New OleDb.OleDbDataAdapter("Select * from tblStudents", objConnection)
Dim objCB As New OleDb.OleDbCommandBuilder(objDA)
Dim objDS As New DataSet()
Public UserID, Password As String
Dim StudentFound As Boolean
Private Sub Retrieve()
objDS.Clear()
objDA.FillSchema(objDS, SchemaType.Source, "tblStudents")
objDA.Fill(objDS, "tblStudents")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnLogin1.Click
UserID = txtUsername.Text
Password = txtPassword.Text
For i As Integer = 1 To (objDS.Tables("tblStudents").Rows.Count)
If CStr(objDS.Tables("tblStudents").Rows(i - 1).Item("Surname")) =
UserID And
CStr(objDS.Tables("tblStudents").Rows(i - 1).Item("Student Number")) =
Password Then
StudentFound = True
frmStudentPortal.txtMCQ.Text = CStr(objDS.Tables("tblstudents").Rows(i - 1).Item("MCQ Result"))
frmStudentPortal.txtProject.Text = CStr(objDS.Tables("tblstudents").Rows(i - 1).Item("Project Result"))
frmStudentPortal.txtOverall.Text = CStr(objDS.Tables("tblstudents").Rows(i - 1).Item("Overall Result"))
frmStudentPortal.lblname.Text = "Welcome " & CStr(objDS.Tables("tblstudents").Rows(i - 1).Item("First Name"))
frmStudentPortal.lblname.Visible = True
MessageBox.Show("Welcome to the System!")
lblStudentIDincorrect.Hide()
lblPasswordIncorrect.Hide()
Me.Hide()
frmStudentPortal.Show()
'Main_Page.Show()
Exit For
End If
txtUsername.Text = String.Empty
txtPassword.Text = String.Empty
Next
If StudentFound = False Then
MessageBox.Show("Access Denied")
lblStudentIDincorrect.Show()
lblPasswordIncorrect.Show()
End If
End Sub