Creating Graph with VB.NET, Part 3: Data Binding Chart to Database

This entry is part 3 of 3 in the series Creating Graph with VB.NET

From Part 1: Basic Chart and Part 2: Customize Chart, you see how to create a basic chart using GUI (no coding). But the chart is static, it’s lack flexibility which means that you cannot change properties or data of the chart while the application is running. Therefore, I will show how to create a chart by coding and bind data from a SQL Server’s database to the Chart control.On this example, I will create a connection to “Northwind” database on this SQL Server “BKKSQL001\INSTANCE01” and query product name and units in stock from “Products” table. Then, I create a chart and bind the query’s result to the chart.

Step-by-step to implement data binding chart to database

  1. Create a new Windows Application project on VB.NET and type name as “SampleDataBindChart“.
    Create New VB.NET's Windows Application
  2. On Form1, open code window and import these libraries. The first two libraries are used for SQL. The last one is used for Chart.
    Imports System.Data
    Imports System.Data.SqlClient
     
    Imports System.Windows.Forms.DataVisualization.Charting

    Import Required Libraries

  3. Type code below on Form1_Load().
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    
            Dim strConn As String = "Data Source=BKKSQL001\INSTANCE01;" & _
                "Initial Catalog=Northwind;Integrated Security=True"
     
            Dim conn As New SqlConnection(strConn)
     
            Dim sqlProducts As String = "SELECT Top 8 ProductName, UnitsInStock FROM Products"
            Dim da As New SqlDataAdapter(sqlProducts, conn)
            Dim ds As New 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")

    Code Explanation:

    • Line 1-2: Define a connection string to connect to a database on SQL Server.
      • Data Source is a SQL Server name.
      • Initial Catalog is a database name.
      • Set Integrated Security=True to use the current user as identity to access the SQL Server database.
    • Line 4: Create a SqlConnection’s object.
    • Line 6: Define SQL query string.
    • Line 7-9: Execute the query and populate result to DataSet’s object.
    • Line 11-14: Create Chart’s objects.
    • Line 15: Add Chart’s object to the form.
    • Line 17-29: Set Chart’s properties (ChartArea, Legend and Series).
    • Line 31-32: Bind column “ProductName” to X-axis and column “UnitsInStock” to Y-axis on the “Series1” Chart.
    • Line 34: Set Chart’s data source to the DataTable in the DataSet’s object.

    Form1_Load's code

  4. Run the project. You see a chart displaying “Product Name” on X-axis and “Unit in Stock” on Y-axis which data is gathered from Northwind database on SQL Server.
    Data Binding Chart to Database
Series Navigation<< Creating Graph with VB.NET, Part 2: Customize Chart

70 Comments

  1. Noob August 10, 2010
  2. Noob August 10, 2010
  3. vik August 14, 2010
  4. jeal4real August 29, 2010
  5. Kashif Gulzar January 16, 2011
  6. Kashif Gulzar January 16, 2011
  7. Kashif Gulzar January 17, 2011
  8. JetSeP February 8, 2011
  9. Mike February 16, 2011
  10. Mike February 16, 2011
  11. Dany February 22, 2011
  12. Sophie February 24, 2011
  13. Dany February 25, 2011
  14. LL February 27, 2011
  15. paula March 8, 2011
  16. linglom March 31, 2011
  17. Gregor August 31, 2011
  18. Gayath Peiris September 6, 2011
  19. lambertlai September 7, 2011
  20. mrhendrik September 7, 2011
  21. tingting September 16, 2011
  22. camry September 29, 2011
  23. betty barnes October 23, 2011
  24. kalla November 18, 2011
  25. Oscar November 26, 2011
  26. SteellNestor December 10, 2011
  27. Jibril Hassan Mohamed Ali December 17, 2011
  28. Jayanthi January 4, 2012
  29. Amar January 20, 2012
  30. Khalid February 17, 2012
  31. slaxsaurina February 22, 2012
  32. Alex February 28, 2012
  33. xawi March 4, 2012
  34. xawi April 11, 2012
  35. Wouter May 18, 2012
  36. kalla May 31, 2012
  37. kalla May 31, 2012
  38. Rosemma July 25, 2012
  39. Muhammas September 14, 2012
  40. Hassaan October 17, 2012
  41. arlene carpizo October 25, 2012
  42. arlene carpizo October 25, 2012
  43. Nancy Alajarmeh February 28, 2013
  44. marvin March 17, 2013
  45. Cindy May 5, 2013
  46. destiny May 13, 2015
  47. linglom May 17, 2015
  48. Mohan October 29, 2015
  49. Mohan October 29, 2015
  50. linglom November 1, 2015
  51. adugna January 22, 2016
  52. linglom January 25, 2016
  53. Marco Cantu January 28, 2016
  54. adu February 25, 2016
  55. JM September 15, 2016
  56. Yogender Sharma September 20, 2016
  57. linglom September 20, 2016
  58. linglom September 21, 2016
  59. NH February 2, 2017
  60. Rajkumar September 20, 2017
  61. Rajkumar September 20, 2017
  62. technopower November 29, 2018
  63. Pramodu May 25, 2019
  64. Soloshow August 28, 2019
  65. Eli October 18, 2021
  66. amy October 18, 2021
  67. Big man October 18, 2021
  68. Antonio Thomas September 9, 2024
  69. Antonio Thomas September 18, 2024
  70. Antonio Thomas September 23, 2024

Leave a Reply