The following is my SQL Server Data Table with the data I will show in the chart.

SQL server Data Table

Image 1.

Here I will show the total number of users by their city (Address). The following is my table in design mode.

table desine view

Image 2.

The script of my table is:

  1. CREATE TABLE [dbo].[Employee](
  2. [CompanyName] [varchar](50) NULL,
  3. [EmployeeCode] [int] NOT NULL,
  4. [EmployeeSupervisorCode] [int] NULL,
  5. [EmployeeName] [varchar](50) NULL,
  6. [ProjectName] [varchar](50) NULL,
  7. [JoiningDate] [datetime] NULL,
  8. [Experience] [varchar](50) NULL,
  9. [Mobile] [varchar](15) NULL,
  10. [Address] [varchar](50) NULL,
  11. [CreatedDate] [datetime] NULL,
  12. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
  13. (
  14. [EmployeeCode] ASC
  15. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  16. ) ON [PRIMARY]
  17. GO
  18. SET ANSI_PADDING OFF
  19. GO
You can drag and drop a chart control from the tool box to your aspx page like the following in my aspx code:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChartControlDemo.aspx.cs" Inherits="ChartControlApplication.ChartControlDemo" %>
  2. <%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
  3. <!DOCTYPE html>
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6. <title>Showing Employee Information in Chart</title>
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. <asp:Chart ID="EmployeeChartInfo" runat="server" Height="450px" Width="550px">
  12. <Titles>
  13. <asp:Title ShadowOffset="3" Name="Items" />
  14. </Titles>
  15. <Legends>
  16. <asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default" LegendStyle="Row" />
  17. </Legends>
  18. <Series>
  19. <asp:Series Name="Default" />
  20. </Series>
  21. <ChartAreas>
  22. <asp:ChartArea Name="ChartArea1" BorderWidth="1" />
  23. </ChartAreas>
  24. </asp:Chart>
  25. </div>
  26. </form>
  27. </body>
  28. </html>
Now, make sure your web.config file has the following peice of code:
  1. <?xml version="1.0"?>
  2. <!--
  3. For more information on how to configure your ASP.NET application, please visit
  4. http://go.microsoft.com/fwlink/?LinkId=169433
  5. -->
  6. <configuration>
  7. <appSettings>
  8. <add key="ChartImageHandler" value="storage=file; timeout=20;" />
  9. </appSettings>
  10. <system.web>
  11. <compilation debug="true" targetFramework="4.5">
  12. <assemblies>
  13. <add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  14. </assemblies>
  15. </compilation>
  16. <httpRuntime targetFramework="4.5"/>
  17. </system.web>
  18. <system.webServer>
  19. <handlers>
  20. <remove name="ChartImageHandler" />
  21. <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST"
  22. path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  23. </handlers>
  24. </system.webServer>
  25. </configuration>
Now the aspx.cs code is:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Web.UI.DataVisualization.Charting;
  10. namespace ChartControlApplication
  11. {
  12. public partial class ChartControlDemo : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. if (!IsPostBack)
  17. {
  18. GetEmployeeChartInfo();
  19. }
  20. }
  21. private void GetEmployeeChartInfo()
  22. {
  23. DataTable dt = new DataTable();
  24. using (SqlConnection con = new SqlConnection(@"Data Source=MyPC\SqlServer2k8;Integrated Security=true;Initial Catalog=Test"))
  25. {
  26. con.Open();
  27. SqlCommand cmd = new SqlCommand("SELECT Address as Name, COUNT(EMPLOYEECODE) AS Total FROM Employee GROUP BY Address", con);
  28. SqlDataAdapter da = new SqlDataAdapter(cmd);
  29. da.Fill(dt);
  30. con.Close();
  31. }
  32. string[] x = new string[dt.Rows.Count];
  33. int[] y = new int[dt.Rows.Count];
  34. for (int i = 0; i < dt.Rows.Count; i++)
  35. {
  36. x[i] = dt.Rows[i][0].ToString();
  37. y[i] = Convert.ToInt32(dt.Rows[i][1]);
  38. }
  39. EmployeeChartInfo.Series[0].Points.DataBindXY(x, y);
  40. EmployeeChartInfo.Series[0].ChartType = SeriesChartType.Pie;
  41. EmployeeChartInfo.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
  42. EmployeeChartInfo.Legends[0].Enabled = true;
  43. }
  44. }
  45. }
Now run your application:

Employee Chart Info
Image 3.