基本上,我已经遇到过这个错误很多次并尝试修复它,但无济于事。我已经四处看看,似乎没有一个解决方案对我有所帮助。我尝试做的是用Access数据库中的数据填充标签。但是,它只会填充7个必填字段,并给出错误消息"索引超出了数组的范围"。有人能够快速查看我的VB代码并给我一个帮助吗?
Private Sub NR1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NR1.Click
Try
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\Flix.Accdb;Persist Security Info=False;")
If cn.State = ConnectionState.Open Then
cn.Close()
End If
cn.Open()
Dim dr1 As OleDbDataReader
Dim com1 As New OleDbCommand
com1.CommandText = "select [Title],[Poster],[Description],[Classification], [Rating], [Stars], [Director], [MakeYear], [Price], [RunningTime] from tbl_newreleases where ID = '" & "1" & "'"
com1.Connection = cn
dr1 = com1.ExecuteReader
If dr1.Read Then
NRTitle.Text = (dr1(9)).ToString()
NRPoster.ImageLocation = (dr1(1)).ToString()
NRDescription.Text = (dr1(2)).ToString()
NRClassification.ImageLocation = (dr1(3)).ToString()
NRStars.ImageLocation = (dr1(4)).ToString()
lblCast.Text = (dr1(5)).ToString
lblDirector.Text = (dr1(6)).ToString
lblYear.Text = (dr1(8)).ToString
lblPrice.Text = (dr1(10)).ToString
lblRunTime.Text = (dr1(7)).ToString
End If
cn.Close()
dr1.Close()
Return
Catch ex As Exception
MsgBox(ex.Message(), MsgBoxStyle.Critical, "Error...")
End Try
End Sub
答案 0 :(得分:1)
OleDbDataReader
的索引从0
开始,因此dr1(10)
根据您的查询不存在
As per your query
dr(0) will be the value of [Title]
dr(1) will be the value of[Poster]
.........
dr(9) will be the value of [RunningTime]
他们不存在dr(10)
。这就是你获得Index Out of Range exception
请按以下方式进行更改
If dr1.Read Then
NRTitle.Text = dr1(8).ToString()
NRPoster.ImageLocation = dr1(0).ToString()
NRDescription.Text = dr1(1).ToString()
NRClassification.ImageLocation = dr1(2).ToString()
NRStars.ImageLocation = dr1(3).ToString()
lblCast.Text = dr1(4).ToString()
lblDirector.Text = dr1(5).ToString()
lblYear.Text = dr1(7).ToString()
lblPrice.Text = dr1(9).ToString()
lblRunTime.Text = dr1(6).ToString()
End If
答案 1 :(得分:0)
select [Title],[Poster],[Description],[Classification], [Rating],
[Stars], [Director], [MakeYear], [Price], [RunningTime]
from tbl_newreleases where ID =
标题= 0海报= 1说明= 2分类= 3评级= 4星= 5 Director = 6 MakeYear = 7 Price = 8 Runningtime = 9
然而,根据列顺序,查询没有多大意义。例如,您似乎将RunningTime的值放入NRTitle.Text,Price的值为lblYear.txt。仍然没有解释为什么Sample给出的答案实际上没有起作用并且阻止了Out of Bounds错误。
If dr1.Read Then
NRTitle.Text = (dr1(9)).ToString()
NRPoster.ImageLocation = (dr1(1)).ToString()
NRDescription.Text = (dr1(2)).ToString()
NRClassification.ImageLocation = (dr1(3)).ToString()
NRStars.ImageLocation = (dr1(4)).ToString()
lblCast.Text = (dr1(5)).ToString
lblDirector.Text = (dr1(6)).ToString
lblYear.Text = (dr1(8)).ToString
lblPrice.Text = (dr1(10)).ToString
lblRunTime.Text = (dr1(7)).ToString
End If
尝试
If dr1.Read Then
NRTitle.Text = (dr1(0)).ToString()
NRPoster.ImageLocation = (dr1(1)).ToString()
NRDescription.Text = (dr1(2)).ToString()
NRClassification.ImageLocation = (dr1(3)).ToString()
NRStars.ImageLocation = (dr1(4)).ToString()
lblCast.Text = (dr1(5)).ToString
lblDirector.Text = (dr1(6)).ToString
lblYear.Text = (dr1(7)).ToString
lblPrice.Text = (dr1(8)).ToString
lblRunTime.Text = (dr1(9)).ToString
End If
然后如果仍然失败,请尝试
If dr1.Read Then
NRTitle.Text = (dr1("Title")).ToString()
NRPoster.ImageLocation = (dr1("Poster")).ToString()
NRDescription.Text = (dr1("Description")).ToString()
....(等等使用名称而不是序数
End If
如果你仍然得到Out of Bounds错误,请通过IF语句调试步骤,看看哪一行导致它跳转到CATCH块。如果在此之后仍然不清楚,我可以建议如下:
1)在您正在使用的数据库引擎中运行查询,检查它是否有效并检查是否有任何值显示为NULL
2)对于IF语句中的每一行,每个行的编码略有不同(第一个显示的例子)
dim NRTitleValue as string = trycast(dr1(0),string)
if NRTitleValue is nothing then
NRTitle.text = ""
else
NRTitle.Text = NRTitleValue
end if
如果在所有这些之后它仍然失败,那么你有一个我多年来从未见过的作为代码猴子的错误!祝你好运,希望你能解决它。