我在尝试检查数据库中的值并从数据库读取文本框时遇到错误。在for i as integer
行,我收到以下错误消息:
未处理的类型' System.NullReferenceException'尽管代码工作较早,但仍发生在VBPROJECT.exe中。
以下是代码:
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