Reputation: 136
I'm trying to reuse my chart but i'm getting the error
A chart element with the name 'Series1' could not be found in the 'SeriesCollection'.
every time i try to requery. i tried to add this 3 line of codes before calling my function but to no avail, any ideas?
Chart1.DataSource = Nothing
Chart1.Series.Clear()
Chart1.ChartAreas.Clear()
My code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'still getting the same error
'Chart1.DataSource = Nothing
'Chart1.Series.Clear()
'Chart1.ChartAreas.Clear()
GetData()
End Sub
Private Sub GetData()
If Not cnninventory.State = ConnectionState.Open Then
cnninventory.Open()
End If
Dim d1 As DateTime = DateTimePicker1.Value
Dim d2 As DateTime = DateTimePicker2.Value
Dim searchsql1 As New OleDb.OleDbDataAdapter("Select * from saleshisTBL where HistDate >= #" & String.Format("{0:MM/dd/yyyy}", d1) & "# and HistDate <= #" & String.Format("{0:MM/dd/yyyy}", d2) & "# ORDER BY HistDate ", cnninventory)
Dim ds As New DataSet
searchsql1.Fill(ds, "saleshisTBL")
Chart1.DataSource = ds.Tables("saleshisTBL")
Dim Series1 As Series = Chart1.Series("Series1")
Series1.Name = "Sales"
Chart1.Series(Series1.Name).XValueMember = "ItemSold"
Chart1.Series(Series1.Name).YValueMembers = "Quantity"
End Sub
Upvotes: 2
Views: 6777
Reputation: 3141
Try below, it should work for you as well
Private Sub GetData()
If Not cnninventory.State = ConnectionState.Open Then
cnninventory.Open()
End If
Dim d1 As DateTime = DateTimePicker1.Value
Dim d2 As DateTime = DateTimePicker2.Value
Dim searchsql1 As New OleDb.OleDbDataAdapter("Select * from saleshisTBL where HistDate >= #" & String.Format("{0:MM/dd/yyyy}", d1) & "# and HistDate <= #" & String.Format("{0:MM/dd/yyyy}", d2) & "# ORDER BY HistDate ", cnninventory)
Dim ds As New DataSet
searchsql1.Fill(ds, "saleshisTBL")
Chart1.DataSource = ds.Tables("saleshisTBL")
Chart1.Series.Clear()
Dim Series1 As New DataVisualization.Charting.Series
With Series1
.Name = "Sales"
.ChartType = SeriesChartType.Bar
.XValueMember = "ItemSold"
.YValueMembers = "Quantity"
End With
Chart1.Series.Add(Series1)
Chart1.Invalidate()
End Sub
Upvotes: 2