2017-04-09 59 views
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 
+1

使用调试器来查找问题。 – SLaks

+0

确保你正在编译32位。将该Load事件代码移到窗体的构造函数中。加载事件有时可能会隐藏异常。 – LarsTech

+0

@SLaks我已经运行了调试器。它说在位置1没有行。这让我认为数据库没有正确加载。 – T4RH33L

回答

0

最后,它和我预期的一样。数据未正确加载。我终于意识到Form1_Load的论点是不正确的。

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventHandler) 

需要的是:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

我将让我的教授和同学们知道,这是不正确。

感谢Bugs的故障排除。至少,我现在知道如何很容易地创建一个SQL连接。我也很感激任何人低估了我的问题,并帮助他们弄清楚这一点。

相关问题