2017-04-09 61 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連接。我也很感激任何人低估了我的問題,並幫助他們弄清楚這一點。

相關問題