Create Dynamic Ajax Pie Chart

Creating dynamic Ajax Pie Chart in Asp.net with Database. To implement Ajax pie chart with database first add AjaxControlToolkit to bin folder.

Download the DLL using the below link

Download DLL

First create a table named country details in sql server with name and value field. Download the script for the Country details table below.

Download Script

Add below code in the aspx page of empty web application.

<%@ Register TagPrefix="ajaxToolkit" Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Pie Chart Example with Database in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="width: 40%">
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <ajaxToolkit:PieChart ID="countrychart" runat="server" ChartHeight="300"
                ChartWidth="450" ChartTitle="World wide Data usage %" ChartTitleColor="#0E426C">
            </ajaxToolkit:PieChart>
        </div>
    </form>
</body>
</html>

 C# code

protected void Page_Load(object sender, EventArgs e)
       {
           DataTable dt = new DataTable();
           using (SqlConnection con = new SqlConnection(SQLConnectionString))
           {
               con.Open();
               SqlCommand cmd = new SqlCommand("select name,total=value from countrydetails order by total desc", con);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               da.Fill(dt);
               con.Close();
           }
           foreach (DataRow dr in dt.Rows)
           {
               countrychart.PieChartValues.Add(new AjaxControlToolkit.PieChartValue
               {
                   Category = dr["name"].ToString(),
                   Data = Convert.ToDecimal(dr["total"]),
               });
           }
       }

 Output

Demo Code

Download Demo