2017-04-09 4 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 

私は私の教授やクラスメートをさせることになる、これは間違っていることを知っています。

トラブルシューティングのためのバグありがとうございます。少なくとも、SQL接続の作成方法はとても簡単です。私はまた、私の質問とこれを理解する上での助けを下落させた人に感謝します。

関連する問題