2013-11-20 2 views
0

사용자가 뷰의 셀을 클릭하면 데이터 그 림보기가있는 프로그램을 만들려고합니다. 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 

데이터베이스 테이블에 연결되어 : 그것은 비주얼 스튜디오 2012 익스프레스에 보이는 http://i.imgur.com/rN0zFwG.png

오류보기 : http://i.imgur.com/9QOQHI1.png

'dgvResults.CurrentCell.Value'의 값은 데이터베이스에서 가져온 영화의 이름입니다 (예 : "12 년 한 노예").

내가 뭘 잘못하고 있니?

감사합니다,
C.

+0

오류는 설명 적입니다. 액세스하려는 열/행이 DB에 있는지 확인하십시오. – varocarbas

+2

Do While Rdr.Read() 또는 intCount <6에서 Do not and OR이 필요하다고 생각합니다. –

+0

Kostas의 좋은 점 ... 왜 읽을 수있는 것 이상으로 계속 읽고 싶습니까? – varocarbas

답변

1

문제는 strFields (intCount) 당신이 독자에게 전달하는 값으로 인해 발생합니다. 유효한 열 인덱스가 아닙니다.

1

당신은 아마 같은 DataReader() 다시 반복하기 전에 필드에 대한 루프합니다 : 그것은 더 이상 때문에 for next 루프의 필요하기 때문에

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을 제거하지 않습니다.

관련 문제