We take a chart between name and age and bind that data to our chart control.

Initial chamber

Step 1: Open Visual Studio 2010 and create an empty Website. Give a suitable name [ChartControl_demo].

Step 2: In Solution Explorer you get your empty website, Add a web form and SQL Database. By going like this –

For Web Form:

ChartControl_demo (Your Empty Website) - Right Click, Add New Item, then Web Form and name it ChartControl_demo.aspx.

For SQL Server Database:

ChartControl_demo (Your Empty Website) - Right Click, Add New Item, SQL Server Database and add Database inside the App_Data_folder.

Database chamber

Step 3: In Server Explorer, click on your database [Database.mdf] - Tables, Add New Table and make table like the following:

Table - tbl_age [Don’t Forget to make ID as IS Identity -- True].

table design

Show table data:

table

Design chamber

Step 4: Open ChartControl_demo.aspx file from the Solution Explorer and start designing your Application:

Here is the Code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
  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></title>
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. <asp:Chart ID="Chart1" runat="server" Palette="None" AntiAliasing="Graphics" Width="563px">
  12. <Series>
  13. <%-- <asp:Series ChartArea="ChartArea1" IsValueShownAsLabel="True" Name="Series1">
  14. </asp:Series>--%>
  15. </Series>
  16. <ChartAreas>
  17. <asp:ChartArea Name="ChartArea1">
  18. <AxisX Title ="name"></AxisX>
  19. <AxisY Title ="age"></AxisY>
  20. <Area3DStyle Enable3D="true" LightStyle="Realistic" />
  21. </asp:ChartArea>
  22. </ChartAreas>
  23. </asp:Chart>
  24. </div>
  25. <p>
  26. <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Load Chart " />
  27. </p>
  28. </form>
  29. </body>
  30. </html>
Code chamber

At last open your ChartControl_demo.aspx.cs file to write the code for making the 3D Chart like we assumed.

Here is the code:

Add these Namespaces:

namespace
  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.Web.UI.DataVisualization.Charting;
  8. using System.Data.SqlClient;
  9. using System.Data;
  10. public partial class Default2 : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. }
  15. protected void Button1_Click(object sender, EventArgs e)
  16. {
  17. SqlConnection con = new SqlConnection(@"Data Source=NiluNilesh;Initial Catalog=mynewdata;Integrated Security=True");
  18. con.Open();
  19. SqlCommand cmd = new SqlCommand("select name, age from tbl_age", con);
  20. SqlDataReader sdr = cmd.ExecuteReader();
  21. Chart1.DataBindTable(sdr, "name");
  22. }
  23. }
Output chamber

Output

Hope you like it. Thank you for Reading. Have a good Day.