After adding the webform1. aspx you will see the following code:
<@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Art.WebForm1" >
<!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></title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
Step 3: Now in Visual Studio just drag and drop the Pie Chart and from the toolbox inside the form tag or use the following code:
<div style="width: 337px; height: 379px;">
<asp:PieChart>
</asp:PieChart>
</div>
Step 4: Now inside the form tag add a Toolkit Script Manager using the following code or just drag and drop the ScriptManager from the Toolbox.
Step 6: Now after creation of the database and table we return to Visual Studio 2010 and write the code for the creation of the pie chart and the retrival of values from the database in webform1.aspx.cs. The code will be as follows:
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;
namespace Art
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
//for sqlserver authentication connection string-(server,databasename,Windows authentication)
{
string connstr = "server=.\\SQLEXPRESS;database=Employee;integreted security=ture";
//class System.Data.SqlClient.SqlConnection -Represent an open connection to a SQL Server database
//initializes instance of class SqlConnection
SqlConnection conn = new SqlConnection(connstr);
//select query for fetch the data from database table population
string query = "select * from population";
//class System.Data.SqlClient. SqlDataAdapter- //Represent a set of data commands and a databse connection that are used to fill
// the System.Data.Dataset and update a sql server database
//Initializes instance of class SqlDataAdapter
SqlDataAdapter da = new SqlDataAdapter(query, conn);
//Initializes instance of System.Data.Dataset class
DataSet ds = new DataSet();
//adds or refreshes rows in the System.Data.Dataset by Fill() Method
da.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
PieChart1.PieChartValues.Add(new AjaxControlToolkit.PieChartValue
{
Category = row["country"].ToString(),
Data = Convert.ToDecimal(row["population"])
});
}
}
}
}
Output
Now all the work has been done, just run the project and see the output.