这是我正在做的家庭作业。我有一个.mdb文件应该连接到我的应用程序,然后我应该能够在记录之间导航。数据库作为数据源连接。但是,当我运行应用程序时,没有填充任何数据,当我按下工具条上的按钮时,我得到一个未处理的IndexOutOfRangeException错误。我试图向教授寻求帮助,但她在整个学期都不存在。我在这做错了什么?我只是寻求帮助,以便集中注意力,以便我可以自己解决这个问题。
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
答案 0 :(得分:0)
In the end, it was as I expected. The data was not loading properly. I finally realized that the Form1_Load
argument was not correct.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventHandler)
Needed to be:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
I will be letting my professor and fellow classmates know that this is incorrect.
Thanks to Bugs for the troubleshooting. At the least, I now know how to create a SQL connection very easily. I also appreciate whomever downvoted my question and their help in figuring this out.