Click here to Skip to main content
15,897,360 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Can anyone know this.

how to solve index was out of range. must be non-negative and less than the size of
the collection?

code is:

What I have tried:

VB.NET
Private Sub btn_Rate_Click(sender As Object, e As EventArgs) Handles btn_Rate.Click
    Label37.Visible = True
    Label39.Visible = True
    Label48.Visible = True
    Label49.Visible = False
    Label51.Visible = False
    Label52.Visible = False
    Label37.Text = "The Chart of " & Label38.Text & " by Rate of Incidents "
    Label48.Text = "From" & " " & Label42.Text & " " & "to" & " " & Label45.Text
    If DropDownList13.SelectedIndex = -1 Then
        Label39.Text = Label40.Text
    Else
        Label39.Text = Label40.Text & Label41.Text
    End If
    btn_Print.Visible = True
    Dim dtRate As DataTable = New DataTable("dtRates")
    For Each cell As TableCell In gvRate.HeaderRow.Cells
        dtRate.Columns.Add(cell.Text.Trim())
    Next
    For Each row As GridViewRow In gvRate.Rows
        dtRate.Rows.Add()
        For i As Integer = 0 To row.Cells.Count = -1
            dtRate.Rows(row.RowIndex)(i) = row.Cells(i).Text.Trim()
        Next
    Next
    Dim departments As List(Of String) = New List(Of String)()
    departments = (From p In dtRate.AsEnumerable() Select p.Field(Of String)("Department")).Distinct().ToList()
    If RateChart.Series.Count() = 1 Then
        RateChart.Series.Remove(RateChart.Series(0))
    End If

    For Each department As String In departments
        Dim UserDepartment = department
        Dim X As Integer()
        X = (From p In dtRate.AsEnumerable()
           Where p.Field(Of String)("Department") = UserDepartment
           Order By p.Field(Of String)("Period_Shown")
           Select Convert.ToInt32(p.Field(Of String)("Period_Shown"))).ToArray()
        Dim Y As Decimal()
        Y = (From p In dtRate.AsEnumerable()
             Where p.Field(Of String)("Department") = UserDepartment
             Order By p.Field(Of String)("Period_Shown")
             Select Convert.ToDecimal(p.Field(Of String)("Rate_Int"))).ToArray()
        RateChart.Series.Add(New Series(department))
        RateChart.Series(department).IsValueShownAsLabel = True
        RateChart.Series(department).BorderWidth = 3
        RateChart.Series(department).ChartType = SeriesChartType.Line
        RateChart.Series(department).Points.DataBindXY(X, Y)
    Next
    RateChart.Legends(0).Enabled = True
End Sub
Posted
Updated 29-Nov-20 22:32pm
v2

You haven't told us which line the error occurs on.

It's also going to depend on your data and chart setup, which we don't have access to.

The error occurs because you are passing an index which is out of bounds to a method or indexer property. You'll need to debug your code to find out why.
 
Share this answer
 
.NET collections have elements with valid indexes between 0 and (N - 1), where N is the number of elements in the collection. So for a three element array, valid indexes will be 0, 1, and 2 only - all other values will cause an "index out of range" error.

Somewhere in that code, you are using an out-of-range index - and we can't tell where because we have no access to your data or the supporting code.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900