Dynamic Chart Control In ASP.NET From Database - Part Seven

Introduction

In this article, I will demonstrate how to dynamically bind repeater control in asp.net from the database. The chart controls enable you to create ASP.NET pages or Windows Forms applications with simple, intuitive, and visually compelling charts for complex statistical or financial analysis.

Step 1

Create database table in sql server 2014.

  1. CREATE TABLE [dbo].[StudentMarks](  
  2.     [ID] [int] IDENTITY(1,1) NOT NULL,  
  3.     [StudentName] [nvarchar](50) NULL,  
  4.     [TotalMarks] [int] NULL,  
  5.  CONSTRAINT [PK_StudentMarks] PRIMARY KEY CLUSTERED   
  6. (  
  7.     [ID] ASC  
  8. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]  
  9. ) ON [PRIMARY]  
  10.   
  11. GO  
  1. CREATE PROCEDURE spGetStudentMarks  
  2. AS  
  3. BEGIN  
  4. SELECT StudentName,TotalMarks from [dbo].[StudentMarks]  
  5. END  

Screenshot database table

ASP.NET 

Step 2

Open Visual Studio 2015 click on New Project and create an empty web application project.

Screenshot for creating new project 1

After clicking on New Project one window will appear, select Web from left panel choose ASP.NET Web Application give a meaningful name of your project then click on OK as shown in below screenshot.

Screenshot for creating new project 2

 

After clicking on OK one more window will appear, choose Empty, check on Web Forms checkbox and click on OK. As shown in the below screenshot.

Screenshot for creating new project 3

Step 3

Double click on webconfig file and database connect. Write the  following line of code.

  1. <connectionStrings>  
  2.   <add name="DBCS" connectionString="data source=DESKTOP-M021QJH\SQLEXPRESS; database=DemoDB; integrated security=true;"/>  
  3. </connectionStrings>  

Step 4 - Add web form in your project

Right click on project select add choose Web Form and click.

Screenshot adding web form 1


After clicking on web form one window will appear; give a name to web form  such as ChartControl or any meaningful name. Click on OK and a web form will be added in the project.

Screenshot adding web form 2

 

Step 5

Add script and styles of Bootstrap in head section of web form.

  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">  
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  3. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>  

Step 6

Drag and drop a Chart Control on web form. Design Chart Control.

  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div class="container py-4">  
  4.             <h4 class="text-uppercase text-center">Chart Control in asp.net</h4>  
  5.             <div class="form-group">  
  6.                 <label>Select Chart:</label>  
  7.                 <asp:DropDownList ID="ddlChart" AutoPostBack="true" runat="server" CssClass="custom-select col-md-4" OnSelectedIndexChanged="ddlChart_SelectedIndexChanged"></asp:DropDownList>  
  8.             </div>  
  9.             <asp:Chart ID="Chart1" runat="server" Width="450">  
  10.                 <Titles>  
  11.                     <asp:Title Text="Total Marks of Students"></asp:Title>  
  12.                 </Titles>  
  13.                 <Series>  
  14.                     <asp:Series Name="Series1" ChartArea="ChartArea1"></asp:Series>  
  15.                 </Series>  
  16.                 <ChartAreas>  
  17.                     <asp:ChartArea Name="ChartArea1">  
  18.                         <AxisX Title="Student Name"></AxisX>  
  19.                         <AxisY Title="Total Marks"></AxisY>  
  20.                     </asp:ChartArea>  
  21.                 </ChartAreas>  
  22.             </asp:Chart>  
  23.         </div>  
  24.     </form>  
  25. </body>  

Complete web form code

  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4. <head runat="server">  
  5.     <title>Chart Control</title>  
  6.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">  
  7.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  8.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12.         <div class="container py-4">  
  13.             <h4 class="text-uppercase text-center">Chart Control in asp.net</h4>  
  14.             <div class="form-group">  
  15.                 <label>Select Chart:</label>  
  16.                 <asp:DropDownList ID="ddlChart" AutoPostBack="true" runat="server" CssClass="custom-select col-md-4" OnSelectedIndexChanged="ddlChart_SelectedIndexChanged"></asp:DropDownList>  
  17.             </div>  
  18.             <asp:Chart ID="Chart1" runat="server" Width="450">  
  19.                 <Titles>  
  20.                     <asp:Title Text="Total Marks of Students"></asp:Title>  
  21.                 </Titles>  
  22.                 <Series>  
  23.                     <asp:Series Name="Series1" ChartArea="ChartArea1"></asp:Series>  
  24.                 </Series>  
  25.                 <ChartAreas>  
  26.                     <asp:ChartArea Name="ChartArea1">  
  27.                         <AxisX Title="Student Name"></AxisX>  
  28.                         <AxisY Title="Total Marks"></AxisY>  
  29.                     </asp:ChartArea>  
  30.                 </ChartAreas>  
  31.             </asp:Chart>  
  32.         </div>  
  33.     </form>  
  34. </body>  
  35. </html>  

Step 7

Right click and view web form code. Write the following code.

  1. using System;  
  2. using System.Web.UI.WebControls;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Configuration;  
  6. using System.Web.UI.DataVisualization.Charting;  
  7.   
  8. namespace BindDataControl_Demo  
  9. {  
  10.     public partial class ChartControl : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.             if (!IsPostBack)  
  15.             {  
  16.                 GetChartData();  
  17.                 GetChartTypes();  
  18.             }  
  19.         }  
  20.   
  21.         private void GetChartTypes()  
  22.         {  
  23.             foreach (int chartType in Enum.GetValues(typeof(SeriesChartType)))  
  24.             {  
  25.                 ListItem li = new ListItem(Enum.GetName(typeof(SeriesChartType), chartType), chartType.ToString());  
  26.                 ddlChart.Items.Add(li);  
  27.             }  
  28.         }  
  29.   
  30.         private void GetChartData()  
  31.         {  
  32.             string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  33.             using (SqlConnection con = new SqlConnection(CS))  
  34.             {  
  35.                 SqlCommand cmd = new SqlCommand("spGetStudentMarks", con);  
  36.                 cmd.CommandType = CommandType.StoredProcedure;  
  37.                 con.Open();  
  38.                 SqlDataReader rdr = cmd.ExecuteReader();  
  39.                 // Retrieve the Series to which we want to add DataPoints  
  40.                 Series series = Chart1.Series["Series1"];  
  41.                 // Loop thru each Student record  
  42.                 while (rdr.Read())  
  43.                 {  
  44.                     // Add X and Y values using AddXY() method  
  45.                     series.Points.AddXY(rdr["StudentName"].ToString(),  
  46.                     rdr["TotalMarks"]);  
  47.                 }  
  48.             }  
  49.         }  
  50.   
  51.         protected void ddlChart_SelectedIndexChanged(object sender, EventArgs e)  
  52.         {  
  53.             // Call Get ChartData() method when the user select a different chart type  
  54.             GetChartData();  
  55.             this.Chart1.Series["Series1"].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), ddlChart.SelectedValue);  
  56.         }  
  57.     }  
  58. }  

Step 8

Run the project ctrl+F5

Screenshot 1