IndexOutOfRangeException unhandled

cameronxt

Member
Joined
Mar 2, 2012
Messages
5
Programming Experience
Beginner
forgive me im not the greatest programmer in the world, im just teaching myself as i go, i have got a program that moves through my database and searches for jobs that are still pending. Once it finds that in my job table it goes to the vehicle table to find the matching vehicle in order to make a linklabel list in a flowout panel. my issues is it keeps trying to go beyond the index of my table and im not entirely sure why.

VB.NET:
   Private Sub PendingListFill()        Dim strSelect As String = _
          "SELECT * FROM Job"
        Dim da As SqlDataAdapter = New SqlDataAdapter(strSelect, cnn)


        ' Load a data set.
        Dim ds As DataSet = New DataSet()
        da.Fill(ds, "Job")
        da.UpdateCommand = New SqlCommandBuilder(da).GetUpdateCommand()


        ' Get a reference to the "JOb" DataTable.
        Dim dt As DataTable = ds.Tables("Job")
        ' Modify one of the records.


        Dim strSelect1 As String = _
          "SELECT * FROM Vehicles"
        Dim da1 As SqlDataAdapter = New SqlDataAdapter(strSelect1, cnn)


        ' Load a data set for vehicles
        Dim ds1 As DataSet = New DataSet()
        da1.Fill(ds1, "Vehicles")
        da1.UpdateCommand = New SqlCommandBuilder(da1).GetUpdateCommand()


        ' Get a reference to the "Vehicles" DataTable.
        Dim dt1 As DataTable = ds1.Tables("Vehicles")
        ' Modify one of the records.


        Dim rowcount As Integer
        Dim rowcount1 As Integer
        Dim currentrow As Integer
        Dim currentrow1 As Integer
        Dim Pending As String
        Dim currentveh As Integer
        Dim VEHID As Integer
        rowcount = dt.Rows.Count
        rowcount1 = dt1.Rows.Count
        currentrow = 0
        currentrow1 = 0
        Pending = ds.Tables("JOb").Rows(currentrow).Item("Pending")


        While currentrow < rowcount


            While Pending Is "false"
                Pending = ds.Tables("JOb").Rows(currentrow).Item("Pending")
                If Pending Is "False" Then
                    currentrow = currentrow + 1
                End If
            End While


            currentrow1 = 0


            VEHID = ds.Tables("Job").Rows(currentrow).Item("VehicleID")
            currentveh = ds1.Tables("Vehicles").Rows(currentrow1).Item(0)


            While (currentrow1 < rowcount1)
                While (currentveh <> VEHID) And (currentrow1 < rowcount1)
                    currentveh = ds1.Tables("Vehicles").Rows(currentrow1).Item(0)


                    If ((currentveh <> VEHID) And (currentrow1 < rowcount1)) Then
                        currentrow1 = currentrow1 + 1
                    End If
                End While


            End While
            ' create a new link
            Dim olink As LinkLabel = New LinkLabel
            ' set properties. 
            olink.Enabled = True
            olink.Text = ds1.Tables("Vehicles").Rows(currentrow1).Item(1) & " " & ds1.Tables("Vehicles").Rows(currentrow1).Item(2) & " " & ds1.Tables("Vehicles").Rows(currentrow1).Item(3)
            olink.Visible = True
            ' use tag to store job number
            olink.Tag = ds.Tables("JOb").Rows(currentrow).Item(0)
            'add link click handler
            AddHandler olink.Click, AddressOf onLinkClick
            ' add to this forms controls collection
            FlowLayoutPanel1.Controls.Add(olink)


            currentrow = currentrow + 1


        End While
    End Sub

my error is occuring on this line of code

VB.NET:
olink.Text = ds1.Tables("Vehicles").Rows(currentrow1).Item(1) & " " & ds1.Tables("Vehicles").Rows(currentrow1).Item(2) & " " & ds1.Tables("Vehicles").Rows(currentrow1).Item(3)


it keeps trying to go to line 4 of my dataset but im not sure why, im sure its something stupid im just over looking and im sorry for the mess of code but any pointers would be great. thank you
 
Last edited:
Ive narrowed my problem down to here

VB.NET:
       While (currentrow1 < rowcount1)
                While (currentveh <> VEHID) And (currentrow1 < rowcount1)
                    currentveh = ds1.Tables("Vehicles").Rows(currentrow1).Item(0)


                    If ((currentveh <> VEHID) And (currentrow1 < rowcount1)) Then
                        currentrow1 = currentrow1 + 1
                    End If
                End While


            End While

what im trying to do with this is cycle through my vehicle database looking for a vehicle id that matches with the job id, if it doesnt find the job id for that vehicle keep going, but it continues on anyways
 
If you want the loop to stop at any point (when you find a match), then use Exit While.

Before incrementing currentrow, first make sure the next row exists (is inside the range), by placing it inside an If block.
 
Back
Top