Chart update with Button event in VB.NET RRS feed

  • Question

  • Hi ,

    I have following code inside button to plot a chart based of dropdownlist selection . It works the first time  selection and won't update after different selection and button click event.


    Dim strConn AsString="Data Source=BKKSQL001\INSTANCE01;"& _ "Initial Catalog=Northwind;Integrated Security=True"Dim conn AsNew SqlConnection(strConn)Dim sqlProducts AsString="SELECT Top 8 ProductName, UnitsInStock FROM Products"Dim da AsNew SqlDataAdapter(sqlProducts, conn)Dim ds AsNew DataSet() da.Fill(ds, "Products")Dim ChartArea1 As ChartArea =New ChartArea()Dim Legend1 As Legend =New Legend()Dim Series1 As Series =New Series()Dim Chart1 =New Chart()Me.Controls.Add(Chart1)   ChartArea1.Name="ChartArea1" Chart1.ChartAreas.Add(ChartArea1) Legend1.Name="Legend1" Chart1.Legends.Add(Legend1) Chart1.Location=New System.Drawing.Point(13, 13) 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="ProductName" Chart1.Series("Series1").YValueMembers="UnitsInStock"   Chart1.DataSource= ds.Tables("Products")

    Please kindly help


    Thursday, May 2, 2019 1:56 PM

All replies

  • You seem to be creating new chart objects each time you click the button.

    You only need to make the chart objects once (chartarea, legend and series). Then you need to update them with new data and redraw the chart.

    When you want to re-plot the chart with new data series you clear the series and fill the series again. The chart will refresh after the new data.

    If you start with drawing a practice chart without using the database you can learn how to draw a chart. Then move to filling the chart data from a data source like database.

    I am not sure how the database source is done. Perhaps you need to redo that somehow or update the datatable and or reset the datasource after the update. I dont know the database part. Perhaps you or someone else knows that part?

    Thursday, May 2, 2019 4:50 PM
  • Hi,

    I want to know what the variables provided by your dropdownlist selection are not reflected in your code.The data bound to Chart1 seems to have not changed.

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Friday, May 3, 2019 5:41 AM