In this article I will explain with an example, how to bind DataTable (DataSet) to Chart control in ASP.Net using C# and VB.Net.
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
HTML Markup
The following HTML Markup consists of an ASP.Net DropDownList, RadioButtonList and Chart control. The DropDownList is populated with countries and when a country is selected, the Chart control is populated with the statistics of orders of different cities in the selected country.
The RadioButtonList is used to toggle the Chart types i.e. Bar and Column charts in the Chart control.
<table border="0" cellpadding="0" cellspacing="0">
            <asp:DropDownList ID="ddlCountries" runat="server">
            <asp:RadioButtonList ID="rblChartType" runat="server" RepeatDirection="Horizontal">
                <asp:ListItem Text="Bar" Value="7" Selected="True" />
                <asp:ListItem Text="Column" Value="10" />
<br />
<asp:Button Text="Populate" runat="server" OnClick="PopulateChart" />
<hr />
<asp:Chart ID="Chart1" runat="server" Height="300px" Width="400px" Visible="false">
        <asp:Title ShadowOffset="3" Name="Items" />
        <asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default"
            LegendStyle="Row" />
        <asp:Series Name="Default" />
        <asp:ChartArea Name="ChartArea1" BorderWidth="0" />
You will need to import the following Namespaces.
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.DataVisualization.Charting;
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.DataVisualization.Charting
Populating the DropDownList
Inside the Page Load event, the DropDownList is populated with Countries from the Orders table of the Northwind database.
protected void Page_Load(object sender, EventArgs e)
    if (!this.IsPostBack)
        string query = "SELECT DISTINCT ShipCountry FROM Orders";
        DataTable dt = GetData(query);
        ddlCountries.DataSource = dt;
        ddlCountries.DataTextField = "ShipCountry";
        ddlCountries.DataValueField = "ShipCountry";
        ddlCountries.Items.Insert(0, new ListItem("Select Country", ""));
private static DataTable GetData(string query)
    string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
        using (SqlCommand cmd = new SqlCommand(query))
            DataTable dt = new DataTable();
            using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
            return dt;
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim query As String = "SELECT DISTINCT ShipCountry FROM Orders"
        Dim dt As DataTable = GetData(query)
        ddlCountries.DataSource = dt
        ddlCountries.DataTextField = "ShipCountry"
        ddlCountries.DataValueField = "ShipCountry"
        ddlCountries.Items.Insert(0, New ListItem("Select Country", ""))
    End If
End Sub
Private Shared Function GetData(ByVal query As String) As DataTable
    Dim constr As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand(query)
            Dim dt As DataTable = New DataTable()
            Using sda As SqlDataAdapter = New SqlDataAdapter(query, con)
            End Using
            Return dt
        End Using
    End Using
End Function
Populating the Bar and Column charts using DataTable
When a Country is selected in the DropDownList, the statistical records of Ship Cities and their Total Orders are fetched from the Orders table.
The populated DataTable has been assigned to the Chart Control’s DataSource property.
Then the ShipCity field has been set as the XValueMember while the Total field has been set as the YValueMembers.
Finally the DataBind function is called, which populates the Chart.
protected void PopulateChart(object sender, EventArgs e)
    Chart1.Visible = ddlCountries.SelectedValue != "";
    string query = string.Format("SELECT ShipCity, COUNT(OrderId) [Total] FROM Orders WHERE ShipCountry = '{0}' GROUP BY ShipCity", ddlCountries.SelectedValue);
    DataTable dt = GetData(query);
    Chart1.DataSource = dt;
    Chart1.Series[0].ChartType = (SeriesChartType)int.Parse(rblChartType.SelectedItem.Value);
    Chart1.Legends[0].Enabled = true;
    Chart1.Series[0].XValueMember = "ShipCity";
    Chart1.Series[0].YValueMembers = "Total";
Protected Sub PopulateChart(ByVal sender As Object, ByVal e As EventArgs)
    Chart1.Visible = ddlCountries.SelectedValue <> ""
    Dim query As String = String.Format("SELECT ShipCity, COUNT(OrderId) [Total] FROM Orders WHERE ShipCountry = '{0}' GROUP BY ShipCity", ddlCountries.SelectedValue)
    Dim dt As DataTable = GetData(query)
    Chart1.DataSource = dt
    Chart1.Series(0).ChartType = CType(Integer.Parse(rblChartType.SelectedItem.Value), SeriesChartType)
    Chart1.Legends(0).Enabled = True
    Chart1.Series(0).XValueMember = "ShipCity"
    Chart1.Series(0).YValueMembers = "Total"
End Sub
Web.Config Modifications
You will need to modify the Web.Config file as following shown in YELLOW in order to use the ASP.Net 4.0 Chart control.
        <add key="ChartImageHandler" value="storage=file;timeout=20;" />
        <add name="conString"
        connectionString="Data Source=.\SQL2005;database=Northwind;Integrated Security=true"/>
        <compilation debug="true" targetFramework="4.0">
                <add assembly="System.Web.DataVisualization, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
                <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            <remove name="ChartImageHandler"/>
            <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
Bind DataTable (DataSet) to Chart control in ASP.Net