Create Simple Polling in Asp.Net using XML File
Creating a simple polling in Asp.Net using XML file.XML to store all the poll options and retrieving all the options and displaying result based on polls options.People are usually willing to take part in Polls because they do not usually require personal information, and they are interested in other peoples’ views.
source code
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="voting.aspx.cs" Inherits="rememberme.voting"
%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style
type="text/css">
.ButtonStyle {
background-color: #9EBEDE;
color: Black;
font-family: verdana;
font-size: 8pt;
}
.BarStyle
{
background-color: #996633;
}
.TablePollResultFoot
{
background-color: #B0C4DE;
font-weight: bold;
height:30px;
font-size: 13px;
}
.gridview
{
border: solid 1px #CCCCCC;
width: 100%;
}
</style>
</head>
<body>
<form id="form1"
runat="server">
<div>
<table style="border:1px
solid #9EBEDE" align="center" cellpadding="0"
cellspacing="0" width="100%">
<tr>
<td align="left">
<b><span style="color:#FF6600"> Online Poll Example
with XML </span> </b>
</td>
</tr>
<tr>
<td height="10px"></td>
</tr>
<tr>
<td >
<b>which team win the IPL 2016?</b>
</td>
</tr>
<tr>
<td height="5px"></td>
</tr>
<tr>
<td align="left">
<asp:RadioButtonList ID="radVote" runat="server"
OnSelectedIndexChanged="radVote_SelectedIndexChanged">
<asp:ListItem>kkr</asp:ListItem>
<asp:ListItem>rcb</asp:ListItem>
<asp:ListItem>kXip</asp:ListItem>
<asp:ListItem>dd</asp:ListItem>
<asp:ListItem>other</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<asp:Button ID="btnVote" runat="server"
Text="Vote"
onclick="btnVote_Click" CssClass="ButtonStyle" />
<asp:Button ID="btnResult" runat="server" Text="Reult"
CssClass="ButtonStyle"
onclick="btnResult_Click" />
<br />
<asp:Label ID="lblStatus" runat="server" />
</td>
</tr>
<tr>
<td align="left">
<asp:GridView runat="server" ID="gvResult"
BackColor="White" CellPadding="4"
EnableModelValidation="True"
AutoGenerateColumns="false"
onrowdatabound="gvResult_RowDataBound" EmptyDataText="No one
submit votes"
OnSelectedIndexChanged="gvResult_SelectedIndexChanged">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099"
/>
<HeaderStyle BackColor="#4682B4" Font-Bold="True"
ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66"
Font-Bold="True" ForeColor="#663399" />
<Columns>
<asp:TemplateField HeaderText="Options"
HeaderStyle-HorizontalAlign="Left"
ItemStyle-Width="20%">
<ItemTemplate>
<asp:Label ID="lblOptions" runat="server"
Text='<%#Bind("OPTION_NAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Votes" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblVotes" runat="server"
Text='<%#Bind("VOTES") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="%"
HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="15%">
<ItemTemplate>
<asp:Label ID="lblpercentage" runat="server"
Text='<%#Bind("PERCENTAGE","{0:f2}")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bar"
HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="55%"
>
<ItemTemplate>
<table runat="server" id="tblBar">
<tr class="BarStyle"><td
height="8px"></td></tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
C# Code
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;
namespace rememberme
{
public partial class voting :
System.Web.UI.Page
{
int Count = 0;
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
btnVote_Click(object sender, EventArgs e)
{
if (radVote.SelectedItem
!= null)
{
InsertVotes(radVote.SelectedItem.ToString());
}
else
{
lblStatus.ForeColor =
Color.Red;
lblStatus.Text =
"Please select at least one option to vote";
}
}
protected void
InsertVotes(string theVote)
{
try
{
XmlDocument xmldoc =
new XmlDocument();
xmldoc.Load(Server.MapPath("XMLFile1.xml"));
XmlElement
parentelement = xmldoc.CreateElement("Vote");
XmlElement votechoice
= xmldoc.CreateElement("Choice");
votechoice.InnerText
= theVote;
parentelement.AppendChild(votechoice);
xmldoc.DocumentElement.AppendChild(parentelement);
xmldoc.Save(Server.MapPath("XMLFile1.xml"));
lblStatus.ForeColor =
Color.Green;
lblStatus.Text =
"Thank you for your vote.";
}
catch
{
lblStatus.Text =
"Sorry, unable to process request. Please try again.";
}
}
protected void readXML()
{
int mCount = 0;
int iCount = 0;
int gCount = 0;
int dCount = 0;
int oCount = 0;
XmlTextReader xmlreader =
new XmlTextReader(Server.MapPath("XMLFile1.xml"));
DataSet ds = new
DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
if (ds.Tables.Count >
0)
{
int dscount =
ds.Tables[0].Rows.Count;
for (int i = 0; i
< dscount; i++)
{
if
(ds.Tables[0].Rows[i][0].ToString() == "kkr")
mCount++;
else if
(ds.Tables[0].Rows[i][0].ToString() == "rcb")
iCount++;
else if
(ds.Tables[0].Rows[i][0].ToString() == "kXip")
gCount++;
else if
(ds.Tables[0].Rows[i][0].ToString() == "dd")
dCount++;
else if
(ds.Tables[0].Rows[i][0].ToString() == "other")
oCount++;
}
double theTotal;
theTotal = mCount +
iCount + gCount + dCount + oCount;
double mPercent;
double oPercent;
double gPercent;
double dPercent;
double otPercent;
mPercent = (mCount /
theTotal) * 100;
string mPercentstr =
mPercent.ToString("N2");
oPercent = (iCount /
theTotal) * 100;
string oPercentstr =
oPercent.ToString("N2");
gPercent = (gCount /
theTotal) * 100;
string gPercentstr =
gPercent.ToString("N2");
dPercent = (dCount /
theTotal) * 100;
string dPercentstr =
dPercent.ToString("N2");
otPercent = (oCount /
theTotal) * 100;
string otPercentstr =
otPercent.ToString("N2");
double
totalpercentage = mPercent + oPercent + gPercent +dPercent + otPercent;
string[] votescount =
{ mCount.ToString(), iCount.ToString(), gCount.ToString(), dCount.ToString(),
oCount.ToString() };
//string[] array = {
mPercent.ToString(), oPercent.ToString(), gPercent.ToString(),
dPercent.ToString(), otPercent.ToString() };
string[] array = {
mPercentstr, oPercentstr, gPercentstr, dPercentstr, otPercentstr };
DataTable dt = new
DataTable();
dt.Columns.Add("OPTION_NAME");
dt.Columns.Add("VOTES");
dt.Columns.Add("PERCENTAGE");
int count = radVote.Items.Count;
Count = count + 1;
for (int i = 0; i
< count; i++)
{
dt.Rows.Add();
dt.Rows[i]["OPTION_NAME"] = radVote.Items[i].ToString();
dt.Rows[i]["VOTES"] =
votescount[i];
dt.Rows[i]["PERCENTAGE"] = array[i];
}
dt.Rows.Add("Total", theTotal, totalpercentage);
gvResult.DataSource =
dt;
gvResult.DataBind();
}
else
{
gvResult.DataSource =
null;
gvResult.DataBind();
}
}
protected void
btnResult_Click(object sender, EventArgs e)
{
readXML();
}
int cnt = 0;
protected void
gvResult_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void
radVote_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void
gvResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType ==
DataControlRowType.DataRow)
{
cnt++;
Label lblpercent =
(Label)e.Row.FindControl("lblpercentage");
HtmlTable tblpercent
= (HtmlTable)e.Row.FindControl("tblBar");
tblpercent.Width =
lblpercent.Text + "%";
if (lblpercent.Text
== "0")
{
tblpercent.Visible = false;
}
if (cnt == Count)
{
e.Row.CssClass =
"TablePollResultFoot";
}
}
foreach (TableCell tc in
e.Row.Cells)
{
tc.Attributes["style"] = "border-color:#CCCCCC";
}
}
}
}
XML Code
<?xml version="1.0" encoding="utf-8"?>
<VotesInformation>
</VotesInformation>
Output
Figure(1) Select anyone of the category based on your wish,Click Vote button.
Figure(2) After submiting vote button,it will be added.
Figure(3) Click Result button to display overall voting result.
Thanks for visiting...