Reputation: 7
Essentially, I've run into this error quite a few times and tried to fix it, but to no avail. I've taken a look around, and it seems none of the solutions have helped me. What I'm trying to do is to populate labels with the data within my Access database. However, it will only populate 7 of the required fields, and gives me the error message "Index was outside the bounds of the array". Would anyone be able to take a quick look at my VB code and give me a hand?
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
Upvotes: 0
Views: 537
Reputation: 500
select [Title],[Poster],[Description],[Classification], [Rating],
[Stars], [Director], [MakeYear], [Price], [RunningTime]
from tbl_newreleases where ID =
Title = 0 Poster = 1 Description = 2 Classification = 3 Rating = 4 Stars = 5 Director = 6 MakeYear = 7 Price = 8 Runningtime = 9
Yet the query makes little sense based on the column order. For example, You appear to be putting the value of RunningTime into NRTitle.Text, the value of Price into lblYear.txt. Still doesn't explain why the answer Sample gave did not actually work and prevent the Out of Bounds error.
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
Try
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
Then if it still fails try
If dr1.Read Then
NRTitle.Text = (dr1("Title")).ToString()
NRPoster.ImageLocation = (dr1("Poster")).ToString()
NRDescription.Text = (dr1("Description")).ToString()
.... (etc etc use the names instead of ordinals
End If
and if you STILL get the Out of Bounds error, debug-step through the IF statement and see which line is causing it to jump to the CATCH block. If it's STILL unclear after that, all I can suggest are the following:
1) Run the query in the database engine you're using, check it works and check if any values appear to be NULL
2) For each line in the IF statement, code each one slightly differently (example for the first shown)
dim NRTitleValue as string = trycast(dr1(0),string)
if NRTitleValue is nothing then
NRTitle.text = ""
else
NRTitle.Text = NRTitleValue
end if
If it still fails after all that, then you have a bug that like of which I've never seen in many years as a code monkey ! Good luck, hope you get it solved.
Upvotes: 0
Reputation:
OleDbDataReader
's index starts from 0
hence dr1(10)
does not exist as per your query
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]
their is no dr(10)
exist. that's why you got Index Out of Range exception
please make the change as follows
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
Upvotes: 1