Saturday, 14 June 2014

Ajax Pie Chart Example in Asp.net


To implement Ajax pie chart first we need to add AjaxControlToolkit to your bin folder and design your aspx page like this


<%@ Register TagPrefix="ajaxToolkit" Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ajax Pie Chart Example</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:40%">
<ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<ajaxToolkit:PieChart ID="pieChart1" runat="server" ChartHeight="300"
ChartWidth="450" ChartTitle="Population % in India"
ChartTitleColor="#0E426C">
<PieChartValues>
<ajaxToolkit:PieChartValue Category="Andhra Pradesh" Data="15" PieChartValueColor="#00ADEE" PieChartValueStrokeColor="black" />
<ajaxToolkit:PieChartValue Category="Tamilnadu" Data="25" PieChartValueColor="#FFC10D" PieChartValueStrokeColor="black" />
<ajaxToolkit:PieChartValue Category="Maharashtra" Data="30" PieChartValueColor="#F16321" PieChartValueStrokeColor="black" />
<ajaxToolkit:PieChartValue Category="Kerala" Data="10" PieChartValueColor="#8BC53E" PieChartValueStrokeColor="black" />
<ajaxToolkit:PieChartValue Category="Uttar Pradesh" Data="30" PieChartValueColor="#896C9E" PieChartValueStrokeColor="black" />
</PieChartValues>
</ajaxToolkit:PieChart >
</div>
</form>
</body>
</html>

If you observe above code for ajax pie chart we declared different properties those are

PieChart Properties

ChartHeight – By using this property we can increase or decrease height of charts.

ChartWidth – By using this property we can increase or decrease width of charts.

ChartTitle – This property allow us to set title of the chart.

ChartTitleColor – By using this property we can set font color of chart title.

PieChart value properties

Category – By using this property we can set name for particular PieChartValue.

Data – By using this property we can set data value for a particular PieChartValue.

PieChartValueColor – This property allow us to set color of segment for a particular PieChartValue..

PieChartValueStrokeColor – By using this property we can set the stroke color of segment for a particular PieChartValue.

Demo:

Friday, 13 June 2014

Insert Data in XML File And Count number of Records(Nodes) in xml file


To count number of nodes from xml file we need to write the code like as shown below


XmlDocument readDoc = new XmlDocument();
readDoc.Load(MapPath("Sample.xml"));
int count = readDoc.SelectNodes("CommentsInformation/Comments").Count;
lblcount.InnerHtml = "Number of Records: "+ count;

If you want to see it in complete example we need to write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="width: 100px">
Name:</td>
<td style="width: 100px">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
Email:</td>
<td style="width: 100px">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /></td>
</tr>
</table>
<br />
<label id="lblcount" runat="server" />
</div>
</form>
</body>
</html>

After that add XML file to your application and give name as "Sample.xml" then add root element in xml file otherwise it will through error. Here I added CommentInformation as root element in XML file.

<?xml version="1.0" encoding="utf-8"?>
<CommentsInformation>
 </CommentsInformation>

After that add this namespace in codebehind

C# Code


using System;
using System.Xml;

After that add below code in code behind


protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("Sample.xml"));
XmlElement parentelement = xmldoc.CreateElement("Comments");
XmlElement name = xmldoc.CreateElement("Name");
name.InnerText = txtName.Text;
XmlElement email = xmldoc.CreateElement("Email");
email.InnerText = txtEmail.Text;
parentelement.AppendChild(name);
parentelement.AppendChild(email);
xmldoc.DocumentElement.AppendChild(parentelement);
xmldoc.Save(Server.MapPath("Sample.xml"));
XmlDocument readDoc = new XmlDocument();
readDoc.Load(MapPath("Sample.xml"));
int count = readDoc.SelectNodes("CommentsInformation/Comments").Count;
lblcount.InnerHtml = "Number of Records: "+ count;
}


Output