0
这是我正在做的家庭作业。我有一个应该连接到我的应用程序的.mdb文件,然后我应该能够在记录之间导航。数据库作为数据源连接。但是,当我运行应用程序时,没有填充数据,当我按下工具栏上的按钮时,出现IndexOutOfRangeException是未处理的错误。我试图寻求教授的帮助,但她在整个学期都不存在。我在这里做错了什么?我只是在寻找帮助,以便把注意力集中在哪里,以便我可以自己解决这个问题。为什么我的表单不能从.mdb文件加载数据?
Public Class Form1
Dim strMemoryConnection As String = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " &
Application.StartupPath & "\memory.mdb"
Dim strSQLMem As String
Dim dtMem As New DataTable()
Dim intTotalRows As Integer
Dim intCurrentRow As Integer
Private Sub displayRecord()
Me.txtTitle.Text = CStr(dtMem.Rows(intCurrentRow)("title"))
Me.txtAuthor.Text = CStr(dtMem.Rows(intCurrentRow)("author"))
Me.txtPublisher.Text = CStr(dtMem.Rows(intCurrentRow)("publisher"))
Me.txtStuff.Text = CStr(dtMem.Rows(intCurrentRow)("stuff"))
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventHandler)
dtMem.Clear()
strSQLMem = "SELECT * FROM Memory"
Dim dataAdapter As New OleDb.OleDbDataAdapter(strSQLMem, strMemoryConnection)
dataAdapter.Fill(dtMem)
dataAdapter.Dispose()
intTotalRows = dtMem.Rows.Count
intCurrentRow = 0
displayRecord()
End Sub
#Region "Tool Strip Button Clicks"
Private Sub btnTop_Click(sender As Object, e As EventArgs) Handles btnTop.Click
intCurrentRow = 1
displayRecord()
End Sub
Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
intCurrentRow = intCurrentRow - 1
If intCurrentRow < 0 Then
intCurrentRow = 1
End If
displayRecord()
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
intCurrentRow = intTotalRows + 1
If intCurrentRow = intTotalRows Then
intCurrentRow = intTotalRows - 1
End If
displayRecord()
End Sub
Private Sub btnBot_Click(sender As Object, e As EventArgs) Handles btnBot.Click
intCurrentRow = intTotalRows - 1
displayRecord()
End Sub
#End Region
End Class
使用调试器来查找问题。 – SLaks
确保你正在编译32位。将该Load事件代码移到窗体的构造函数中。加载事件有时可能会隐藏异常。 – LarsTech
@SLaks我已经运行了调试器。它说在位置1没有行。这让我认为数据库没有正确加载。 – T4RH33L