2013-11-20 69 views
0

我試圖創建一個程序,它具有datagridview,當用戶單擊視圖中的單元格時,它會查找一個SQL數據庫,從同一記錄中的其他字段獲取信息,並自動填充表單中相應的文本框(通過操縱字段名稱完成)。從VB.net連接到SQL數據庫時,「沒有數據存在行/列」

然而對於某些原因,我得到一個錯誤信息說: 「InvalidOperationException異常是未處理」 「不存在數據的行/列」

下面是相關的這部分程序代碼:

Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvResults.CellMouseClick 
    ' Set values in the text boxes to both corresponding to the film. 
    Dim strFields() As String = {"ID", "fName", "fGenre", "fSynopsis", "fAgeRating", "fActorsActresses", "fWebRating", "fNoReservations", "fWeeksRunning"} 

    Dim Con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ApplicationData.accdb;Persist Security Info=False;") 
    Con.Open() 'Open the connection 
    Dim Cmd As New OleDbCommand(StringBuilderCommand("*", "Films", dgvResults.CurrentCell.Value, "fName"), Con) 'Create a string by calling the StringBuilderCommand to combine the parameters together with quotes. 

    Cmd.CommandType = CommandType.Text 
    Dim Rdr As OleDbDataReader = Cmd.ExecuteReader() 

    Dim intCount As Integer = 4 ' Create a loop variable. 
    Do While Rdr.Read() Or intCount < 6 ' While this statement is 'TRUE', e.g. there is a valid record. 

     strResult = "txt" & strFields(intCount).Replace("f", "") 'Remove any instances of 'f', e.g. the prefix of the string. 

     txtActorsActresses.Text = StringBuilderCommand("*", "Films", dgvResults.CurrentCell.Value, "fName") 
     Me.Controls(strResult).Text = Rdr.Item(strFields(intCount)) ' Suspect the error lies here. 

     'Set the text-box to the correct value from the database. 
     'This will allow me to go through several text boxes, and grab their corresponding values from the database. 

     intCount = intCount + 1 

     'Current error is because it cannot find any data beyond the first field taken. 
     'I have no idea why this is. But if I change the starting intCount value, it will successfully take a different value. 
    Loop 

    Rdr.Close() 'Cleaning up. 
    Cmd.Dispose() 
    Con.Close() 

    WebBrowser1.Navigate(dgvResults.CurrentCell.Value.Replace(" ", ".") & ".movie.poster.new.jpg.to") 'Grab the movie poster off the internet corresponding to the films name. 
End Sub 

Private Function StringBuilderCommand(Field, Table, CurrentCellValue, SearchParameter) 
    'Creates a suitable SQL string. 
    Dim MyStringBuilder As New StringBuilder("SELECT ") 
    MyStringBuilder.Append("*") ' Append the parameter 'Field'. 
    MyStringBuilder.Append(" FROM ") ' Append the SQL command 'FROM'. 
    MyStringBuilder.Append(Table) ' Append the parameter 'Table'. 
    MyStringBuilder.Append(" WHERE ") ' Append the SQL command 'WHERE'. 
    MyStringBuilder.Append(SearchParameter) ' Append the parameter 'SearchParameter'. 
    MyStringBuilder.Append("=""") 
    MyStringBuilder.Append(CurrentCellValue) ' Append the parameter 'CurrentCellValue', representing the cell selected. 
    MyStringBuilder.Append("""") 'Append a quotation mark. 

    Return MyStringBuilder.ToString() ' Return it to the main program. 

End Function 

數據庫中的表連接到:http://i.imgur.com/rN0zFwG.png

錯誤的觀點,因爲它看起來在Visual Studio 2012個快遞:http://i.imgur.com/9QOQHI1.png

'dgvResults.CurrentCell.Value'的值是從數據庫中取出的影片的名稱(例如, 「爲奴十二年」)。

我在做什麼錯?

謝謝,
C.

+0

該錯誤非常具有描述性:確保您嘗試訪問的列/行在數據庫中完全存在。 – varocarbas

+2

我認爲在做雖然Rdr.Read()或intCount <6你需要AND而不是或 –

+0

科斯塔斯好點...爲什麼你想繼續閱讀超越可以閱讀? – varocarbas

回答

1

問題是由strFields(intCount)要傳遞到閱讀器的值而引起的。這不是有效的列索引。

1

你可能想上的字段環路上DataReader()再次循環,像以前一樣:

Do While Rdr.Read() 

    For intCount as Integer = 4 to 6 
     strResult = "txt" & strFields(intCount).Replace("f", "") 
     txtActorsActresses.Text = StringBuilderCommand("*", "Films", dgvResults.CurrentCell.Value, "fName") 
     Me.Controls(strResult).Text = Rdr.Item(strFields(intCount)) 
    Next 

Loop 

我刪除了Dim intCount As Integer = 4,因爲它不再需要因爲for next循環。