Creating Graph with VB.NET, Part 3: Data Binding Chart to Database
Programming, VB.NET May 27th, 2010- Creating Graph with VB.NET, Part 1: Basic Chart
- Creating Graph with VB.NET, Part 2: Customize Chart
- Creating Graph with VB.NET, Part 3: Data Binding Chart to Database
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
- Create a new Windows Application project on VB.NET and type name as “SampleDataBindChart“.

- 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
- 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.
- Line 1-2: Define a connection string to connect to a database on SQL Server.
- 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.

Related post
- Creating Graph with VB.NET, Part 1: Basic Chart This series shows how to create chart or graph with VB.NET using Microsoft Chart Controls for .NET Framework. I divide...
- Creating Graph with VB.NET, Part 2: Customize Chart On Part 1: Basic Chart, I show how to create a basic chart with default chart type and color style....
Related posts:






August 10th, 2010 at 4:56 am
Good afternoon,
I have a problem with my chart. The x axis does not show all the labels.
I thought it was the size of the chart, but to increase it only shows the font bigger, not the space to display x-axis labels.
what can i do?
August 10th, 2010 at 4:58 am
PD: Good job, this post is very helpful
August 14th, 2010 at 7:04 am
Good afternoon it hears in your code marks error to me in Me.Controls.Add (Chart1) in Chart1 says to me that A value of type ‘ System.Windows.Forms.DataVisualization.Charting.C hart’ it is not possible to be turned into ‘ System.Web.UI.Co ntrol’. Can you help me?
August 29th, 2010 at 8:41 pm
GOOD ONE!!!!. Helpful….
January 16th, 2011 at 6:37 am
Please send me the answers that u provide to both the above mentioned guys.I have a table with fields Name,Absents and Presents.I wanna show a bar graph with complete Names on x-axis and each name having a red bar for absents and a blue bar for presents.Please help me.
January 16th, 2011 at 6:39 am
Should i use two Series ?
January 17th, 2011 at 11:41 pm
You are great man.I have used your code and i have succeeded.Please put on more tutorials on how to draw pie charts and 3D Graphs.Thank you very much.
February 8th, 2011 at 3:50 am
Hello, im sad because i cant see how to make the chart show all the names in x axis. it only show 4 names .. please help-me
February 16th, 2011 at 10:07 am
This Works fine.
I just wanted to ask if there is a way to use line graph instead of bars and which are the properties to do it.
Also, my title is not displayed. Can you help with this, please?
February 16th, 2011 at 10:12 am
OK, I found, it is not the Chart1, but Series1 property:
Series1.ChartType = SeriesChartType.Line.
Thanks a lot for this tutorial!
February 22nd, 2011 at 4:37 am
Hi,
Thank you for the Very good tutorial,
but m having the same problem with the x axis, The x axis does not show all the labels.
Can you please help me?
maybe it has to do with scale or font size!
February 24th, 2011 at 6:21 am
I have used your tutorial but i have variables in my SQL statement, comoing from a combo box, i want to be able to update the chart is this possible? I have tried update and refresh but the chart remains the same.
February 25th, 2011 at 3:17 am
Hi,
I fixed my problem. just need to add the following line:
Chart1.ChartAreas(“ChartArea1″).AxisX.Interval = 1
Best Regards,
Dany
February 27th, 2011 at 8:18 pm
What is the database of this chart. tnx
March 8th, 2011 at 8:32 pm
System.Windows.Forms.DataVisualization.Charting = there were some errors regarding this.
March 31st, 2011 at 10:40 am
Hi, LL
The database is Northwind. You can see that I defined it in ‘strConn’ variable in code.
Hi, Paula
You have to install Microsoft Chart Controls and add reference the DLL to your project. The control can be download at Microsoft Chart Controls for Microsoft .NET Framework 3.5.
August 31st, 2011 at 12:16 pm
Hi,
Great tutorial. That’s what I needed.Thanks alot.
September 6th, 2011 at 4:27 pm
Great tutorial. Thanks.
my sample view two chart, what happen?
September 7th, 2011 at 2:10 pm
how to plot the grid if read data from the serial com? Thanks
September 7th, 2011 at 4:30 pm
chart1.series.item(“Series1″.points.item(“Point1″).YValue
–> how to give value to this point?
thanks for your code.. is very helpful.. great job..
September 16th, 2011 at 11:38 am
Thanks for your tutorial! but i want to get the data in the microsoft access and then do to input them in a table. this is the problem. i followed your step and change the sql to Oledb..well. looks like it still doesn’t work,how to solve. pls help me !
September 29th, 2011 at 9:06 pm
ok gan maksih semoga ini bermanfaat untuk ane…
October 23rd, 2011 at 12:29 am
thanks for this!
November 18th, 2011 at 2:19 pm
every thing is blank. only series name show.
please help
November 26th, 2011 at 10:43 am
Great, however, how do you make scatter plots where the value of x is not just 1,2,3… like in the examples, but any number. That is, plots of a series of (x,y)points.
December 10th, 2011 at 5:12 am
woooooooooooow!!!!!!
EXCELLENT!
December 17th, 2011 at 11:00 pm
It’s the one that i already searching for this
January 4th, 2012 at 6:23 pm
Hi,
Could you please tell me which namespace and dll we have to add for this line chart.
Thanks
January 20th, 2012 at 10:10 pm
Hey,
Can you tell me/direct me to how I can print the chart once I make it?
Thanks