Question Charting not updating Issue

martin.s.ransome

Active member
Joined
May 29, 2011
Messages
25
Programming Experience
5-10
I have a combobox from which the user can select a student, I also have a button from which the user can generate a graph of the student's performance. The graph shows up perfectly but when I select another student and click the button nothing happens. The graph remains the same, no error is thrown.

If you can please Help,

and correct the error in my ways.
 
Did you use code?
 
Yes I did

VB.NET:
    Private Sub ButtonGenGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGenGraph.Click        Dim sqlSelectGrades As String
        Dim strCN As String


        If cn.State = ConnectionState.Closed Then
            strCN = "Data Source=TABS01\TABSDB01;Initial Catalog=SIMS;Persist Security Info=True;User ID=simssysadmin;Password=sims"
            cn.ConnectionString = strCN
            cn.Open()
        End If


        sqlSelectGrades = "SELECT StudentName as Students, avg(grade) Grade FROM studentnames sn, studentgrades sg, subjects s " _
        + " WHERE sn.StudentID = sg.StudentID AND  s.id = sg.subjectid AND s.subjectname = 'Mathematics' AND sn.studentid = " & Me.ComboBoxStudents.SelectedItem.ToString & " group by sn.StudentName " _
        + " UNION SELECT 'Class Average', avg(Grade) FROM studentgrades sg, subjects s WHERE s.id = sg.subjectid AND s.subjectname = 'Mathematics'"


        Dim da As New SqlDataAdapter(sqlSelectGrades, cn)
        Dim ds As New DataSet


        da.Fill(ds, "StudentGrades")


        Dim ChartArea1 As New ChartArea
        Dim chart1 As New Chart
        Dim Legend1 As Legend = New Legend()
        Dim Series1 As Series = New Series()




        Me.Controls.Add(chart1)


        ChartArea1.Name = "ChartArea1"
        chart1.ChartAreas.Add(ChartArea1)
        Legend1.Name = "Legend1"
        chart1.Legends.Add(Legend1)
        chart1.Location = New System.Drawing.Point(100, 250)
        chart1.Name = "Chart1"
        Series1.ChartArea = "ChartArea1"
        Series1.Legend = "Legend1"
        Series1.Name = "Series1"
        chart1.Series.Add(Series1)
        chart1.Size = New System.Drawing.Size(800, 400)
        chart1.TabIndex = 0
        chart1.Text = "Chart1"


        chart1.Series("Series1").XValueMember = "Students"
        chart1.Series("Series1").YValueMembers = "Grade"


        chart1.DataSource = ds.Tables("StudentGrades")


        'chart1.Visible = True
    End Sub
 
You are creating a new Chart control each click, which next gets behind the previous. Either use the existing control, or Dispose it before adding a new one. Former option is probably best, where you replace most of that code by simply configuring the control in designer. After setting datasource call charts methods DataBind and Update.
 
OK So I have found these:

VB.NET:
chart1.Series.Dispose()        chart1.Series.Clear()
        chart1.ChartAreas("ChartArea1").Dispose()

But I imagine that I have to test whether or not the series has data and how do I do that?
 
Why do that? You aren't changing any series or ChartAreas so why destroy it.
 
I do not know if this was posted cause I'm not seeing it so I'm gonna say it again. dotnet newbie, VB6 Expert. I do not have a clue. Please assist.

Thank you.
 
Configure your Chart in designer. In ButtonGenGraph click handler get data, set DataSource, then call the charts DataBind and Update methods.
 
Back
Top