[Solved] VB.Net For Each loop Error: Collection was modified enumeration operation may not execute

hsku6482
 
on Dec 01, 2020 08:02 PM
1197 Views

Hello,

I am using Vb.net(VS2010 Express) to Create a chart with multi Series in a table with selected data in MS SQL Server. After seeked assistant from Dharmendr my concept is clear.

However, I have other error in this application. When I debug it, it have an error about Collection was modified; enumeration operation may not execute.

    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
                Dim j As Integer = i
                dtRate.Rows(row.RowIndex)(j) = row.Cells(j).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 department1 As String = department
            Dim X As Integer()
            X = (From p In dtRate.AsEnumerable()
               Where p.Field(Of String)("Department") = department1
               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") = department1
                 Order By p.Field(Of String)("Period_Shown")
                 Select Convert.ToDecimal(p.Field(Of String)("Rate_Int"))).ToArray()
            departments.Add(department1)
            RateChart.Series.Add(New Series(department1))
            RateChart.Series(department1).IsValueShownAsLabel = True
            RateChart.Series(department1).BorderWidth = 3
            RateChart.Series(department1).ChartType = SeriesChartType.Line
            RateChart.Series(department1).Points.DataBindXY(X, Y)
        Next
        RateChart.Legends(0).Enabled = True
    End Sub

Thanks for everyone.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 02, 2020 03:35 AM
hsku6482 says:
For Each department As String In departments
        Dim department1 As String = department
        Dim X As Integer()
        X = (From p In dtRate.AsEnumerable()
           Where p.Field(Of String)("Department") = department1
           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") = department1
             Order By p.Field(Of String)("Period_Shown")
             Select Convert.ToDecimal(p.Field(Of String)("Rate_Int"))).ToArray()
        departments.Add(department1)
        RateChart.Series.Add(New Series(department1))
        RateChart.Series(department1).IsValueShownAsLabel = True
        RateChart.Series(department1).BorderWidth = 3
        RateChart.Series(department1).ChartType = SeriesChartType.Line
        RateChart.Series(department1).Points.DataBindXY(X, Y)
    Next

Change with below code.

For Each department As String In departments.ToList()
    Dim department1 As String = department
    Dim X As Integer()
    X = (From p In dtRate.AsEnumerable()
            Where p.Field(Of String)("Department") = department1
            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") = department1
            Order By p.Field(Of String)("Period_Shown")
            Select Convert.ToDecimal(p.Field(Of String)("Rate_Int"))).ToArray()
    departments.Add(department1)
    RateChart.Series.Add(New Series(department1))
    RateChart.Series(department1).IsValueShownAsLabel = True
    RateChart.Series(department1).BorderWidth = 3
    RateChart.Series(department1).ChartType = SeriesChartType.Line
    RateChart.Series(department1).Points.DataBindXY(X, Y)
Next