Dynamic Ajax Pie Chart in Asp.net with Database

Ajax pie chart
Introduction:
This article explains how to create dynamic Ajax pie chart in asp.net with database using c#, or create pie chart in asp.net with database example in  c#.

First design table one table countrydetails in your database like as shown below

To implement Ajax pie chart with database first we need to add AjaxControlToolkit to your bin folder and design your aspx page like this
Aspx Page:
----------------------------------------------------------------------------------------------------------------------------
 <%@ Register TagPrefix="ajaxToolkit" Assembly="AjaxControlToolkit" 
Namespace="AjaxControlToolkit" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Pie Chart Example with Database in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:40%">
<ajaxToolkit:ToolkitScriptManager ID="scriptmanager1" runat="server" />
<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("Data Source=vepsh;Integrated Security=true;Initial Catalog=SampleDB")) 
 
{
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"]),
});
}
-----------------------------------------------------------------------------------
Ashwani
Ashwani

This is a short biography of the post author. Maecenas nec odio et ante tincidunt tempus donec vitae sapien ut libero venenatis faucibus nullam quis ante maecenas nec odio et ante tincidunt tempus donec.

No comments:

Post a Comment