繁体   English   中英

从VB.net连接到SQL数据库时“行/列没有数据”

[英]“No data exists for the row/column” when connecting to SQL database from VB.net

我正在尝试创建一个具有datagridview的程序,当用户单击视图中的单元格时,它会查找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 Express中查看错误的视图: http://i.imgur.com/9QOQHI1.png

'dgvResults.CurrentCell.Value'的值是从数据库中取出的电影的名称(例如“12年奴隶”)。

我究竟做错了什么?

谢谢,
C。

问题是由传递给读者的strFields(intCount)的值引起的。 它不是有效的列索引。

您可能希望在再次在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循环不再需要它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM