In this article I’m going to explain how to
bind chart control from database in ASP.NET using C#.
The
Chart controls enable you to create ASP.NET pages with simple, intuitive, and
visually compelling charts for complex statistical or financial analysis
Here I’ll show you how to bind chart from SQL Server database. You
could follow the steps given below.
Step-1:
First drag and drop Chart control from
ToolBox. In web.config file changes will be made after drag and drop Chart
control. We don’t need to configure web.config file manually.

Sample code(web.config):
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\;"/>
</appSettings>
<system.webServer>
<handlers>
<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=4.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"/>
</handlers>
</system.webServer>
<system.web>
<httpHandlers>
<add path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler,
System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</controls>
</pages>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.DataVisualization,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></assemblies></compilation>
</system.web>
</configuration>
Step-2:
Then
we have to modify Chart control like this
Sample
code(design):
<asp:Chart ID="Chart1" runat="server"
Height="300px"
Width="400px"
>
<Titles>
<asp:Title ShadowOffset="3"
Name="Items"
/>
</Titles>
<Legends>
<asp:Legend Alignment="Center"
Docking="Bottom"
IsTextAutoFit="False"
Name="Default"
LegendStyle="Row"
/>
</Legends>
<Series>
<asp:Series Name="Default"
/>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1"
BorderWidth="0"
/>
</ChartAreas>
</asp:Chart>
Designer
source code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI.DataVisualization.Charting"
TagPrefix="asp"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET
chart</title>
</head>
<body>
<form id="form1" runat="server">
<div style="padding-left:200px">
<asp:Chart ID="Chart1" runat="server" Height="300px" Width="400px" >
<Titles>
<asp:Title ShadowOffset="3"
Name="Items"
/>
</Titles>
<Legends>
<asp:Legend Alignment="Center"
Docking="Bottom"
IsTextAutoFit="False"
Name="Default"
LegendStyle="Row"
/>
</Legends>
<Series>
<asp:Series Name="Default"
/>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1"
BorderWidth="0"
/>
</ChartAreas>
</asp:Chart>
</div>
</form>
</body>
</html>
C#
code:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using System.Data;
using
System.Data.SqlClient;
using
System.Web.UI.DataVisualization.Charting;
public partial class _Default : System.Web.UI.Page
{
SqlConnection
conn = new SqlConnection("Data Source=SPIDER;Initial
Catalog=Northwind;Integrated Security=True");
protected
void Page_Load(object
sender, EventArgs e)
{
if (!IsPostBack)
{
BindChart();
}
}
protected
void BindChart()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
conn.Open();
string cmdstr = "select top 6
Country, COUNT(CompanyName) [Total Suppliers] from [Suppliers] group by
Country";
SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn );
adp.Fill(ds);
dt = ds.Tables[0];
string[] x = new string[dt.Rows.Count];
int[] y = new int[dt.Rows.Count];
for (int i = 0; i <
dt.Rows.Count; i++)
{
x[i] =
dt.Rows[i][0].ToString();
y[i] = Convert.ToInt32(dt.Rows[i][1]);
}
Chart1.Series[0].Points.DataBindXY(x,y);
Chart1.Series[0].ChartType = SeriesChartType.Column;
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
Chart1.Legends[0].Enabled = true;
}
}