Chart Control In ASP.NET

In this article, we will learn about how we can use Chart control in ASP.NET.

Chart control is used to create dynamic graphical charts within minutes.

This is a server-side control, To implement Chart we can drag it from the toolbox of visual studio or ASP.NET provides its own tag to implement Chart. The example is given below.

<asp:Chart ID="Chart1" runat="server">
        <asp:Series Name="Series1"></asp:Series>
        <asp:ChartArea Name="ChartArea1"></asp:ChartArea>


In this example, we are implementing a chart using Chart control.

Create a new project and select the ASP.NET Empty Web Site.

Here, we create StudentInfo table with given fields.

Firstly click on View menu -> Server Explorer to access a database server either use Ctrl+Alt+S.

Now, right-click on Data Connections -> Add Connection.

Now simply choose (or enter) the server name, enter server authentication details (if required), choose the database name, and click Test Connection then click OK.

The connection will be entered into the Server Explorer window. Now, choose and right-click on Connection -> Properties (Alt+Enter) then copy connection string from Properties window -> Connection -> Connection String.

Now, open Web.config file from Solution Explorer window and then paste connection string as shown below.

    <add name="myConnectionString" connectionString="Data Source=DESKTOP-78L7I55;Initial Catalog=Student;User ID=sa;Password=vision" />

Now, right-click the project name (CRUD Operations) in the Solution Explorer and select Add -> Add New Item.

Now, let’s add a new Default.aspx file, select Web Form and click Add.

Open the Default.aspx file and add the code in it.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="System.Web.DataVisualization, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
            <asp:Chart ID="Chart1" runat="server">
                    <asp:Legend Alignment="Center" Docking="Bottom" />
                    <asp:Series Name="Student's Age" XValueMember="Name" YValueMembers="Age">
                    <asp:ChartArea Name="ChartArea1">

Open the Default.aspx.cs file and add the code in it.

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da;
    string qry;
    protected void Page_Load(object sender, EventArgs e)
        qry = "Select * from StudentInfo";
        cmd = new SqlCommand(qry, cn);
        DataSet ds = new DataSet();
        da = new SqlDataAdapter(cmd);
        Chart1.DataSource = ds;

That’s it.


Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories