Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - AJAX - Creating Online Poll System with Percentage Results C#
Creating Online Poll System with Percentage Results C#


ASP.NET AJAX Tutorial

This tutorial will show you how to create an online poll system which makes use of AJAX, XML and LINQ and will provide percentage results. C#

Poll systems are very useful for gathering a large amount of data very quickly. They usually require no registration, and are mainly used to gather such data as opinions or trends. 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.

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

In this tutorial, we will show how we can create an online voting system where anyone can vote, and we can see the results of the poll - including a percentage of the answers.
The system will make use of XML and AJAX. We start by creating the ASPX page and the XML structure, adding in the ScriptManager and the UpdatePanel to allow us to use AJAX:

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> </ContentTemplate>
</asp:UpdatePanel> </form>

<?xml version="1.0" encoding="utf-8"?>
<Poll>
<Vote>
<Name>Paul</Name>
<Choice>Obama</Choice>
</Vote>
</Poll>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

Next, we add our controls to the ContentTemplate of the UpdatePanel. We will be adding a label, a textbox, a radiobuttonlist, two buttons, and a literal control. Our ASPX page will now look something like this:

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Literal ID="litResults" runat="server" visible="false"/>
What is your name? <asp:TextBox ID="txtName" runat="server" /><br />
Who is your favorite Candidate?<br />
<asp:RadioButtonList ID="radVote" runat="server">
<asp:ListItem>Obama</asp:ListItem>
<asp:ListItem>McCain</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="butVote" runat="server" Text="Vote"
onclick="butVote_Click" /><br />
<asp:Label ID="lblStatus" runat="server" /><br />
<asp:Button ID="butResults" runat="server" Text="Show Results"
onclick="butResults_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</form>

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

We have one button to submit the vote, and another to read the results. We will be using an XML file to store the votes. Notice that the buttons already have their onclick attributes set to methods in the code-behind. We will first code the vote button:

protected void butVote_Click(object sender, EventArgs e)
{
if (txtName.Text == "")
lblStatus.Text = "Please enter your name.";
else if (radVote.SelectedItem == null)
lblStatus.Text = "Please vote.";
else
countVote(radVote.SelectedItem.ToString());
}

This method simply checks to see that the user has filled out the required controls, and then calls the countVote method to add the vote to the XML file:

protected void countVote(string theVote)
{
try
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("Poll.xml"));

xmlDoc.Element("Poll").Add(new XElement("Vote", new XElement("Name", txtName.Text),
new XElement("Choice", theVote)));

xmlDoc.Save(Server.MapPath("Poll.xml"));
lblStatus.Text = "Thank you for your vote.";
readXML();
}
catch
{
lblStatus.Text = "Sorry, unable to process request. Please try again.";
}
}

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

We use a Try and Catch statement to catch any errors, and we use LINQ to add the vote and name to the XML file. Then we call the final method, readXML to output the current results to the browser:

protected void readXML()
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("Poll.xml"));

var votes = from vote in xmlDoc.Descendants("Vote")
select new
{
Name = vote.Element("Name").Value,
Vote = vote.Element("Choice").Value,
};

int mCount;
int oCount;
mCount = 0;
oCount = 0;

foreach (var vote in votes)
{
if (vote.Vote == "McCain")
mCount++;
else if (vote.Vote == "Obama")
oCount++;
}

double theTotal;
theTotal = mCount + oCount;
double mPercent;
double oPercent;
mPercent = (mCount/theTotal)*100;
oPercent = (oCount/theTotal)*100;

litResults.Visible = true;
litResults.Text = "Obama: " + oCount + " votes (" + oPercent + "%).<br />";
litResults.Text = litResults.Text + "McCain: " + mCount + " votes (" + mPercent + "%).<br /><br />";
}

This method opens up the XML file for access, and then using LINQ again, we select all the data and looping through each element, we count how many votes are for each candidate, using two variables. Next, we do a simple calculation on each variable to find the percentage of the overal votes. Finally, we display the results in the literal control.
We can also add to the Show Results button to call this same method:

protected void butResults_Click(object sender, EventArgs e)
{
readXML();
}

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

The entire code-behind will look something like this:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void butVote_Click(object sender, EventArgs e)
{
if (txtName.Text == "")
lblStatus.Text = "Please enter your name.";
else if (radVote.SelectedItem == null)
lblStatus.Text = "Please vote.";
else
countVote(radVote.SelectedItem.ToString());
}

protected void countVote(string theVote)
{
try
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("Poll.xml"));

xmlDoc.Element("Poll").Add(new XElement("Vote", new XElement("Name", txtName.Text),
new XElement("Choice", theVote)));

xmlDoc.Save(Server.MapPath("Poll.xml"));
lblStatus.Text = "Thank you for your vote.";
readXML();
}
catch
{
lblStatus.Text = "Sorry, unable to process request. Please try again.";
}
}

protected void readXML()
{
XDocument xmlDoc = XDocument.Load(Server.MapPath("Poll.xml"));

var votes = from vote in xmlDoc.Descendants("Vote")
select new
{
Name = vote.Element("Name").Value,
Vote = vote.Element("Choice").Value,
};

int mCount;
int oCount;
mCount = 0;
oCount = 0;

foreach (var vote in votes)
{
if (vote.Vote == "McCain")
mCount++;
else if (vote.Vote == "Obama")
oCount++;
}

double theTotal;
theTotal = mCount + oCount;
double mPercent;
double oPercent;
mPercent = (mCount/theTotal)*100;
oPercent = (oCount/theTotal)*100;

litResults.Visible = true;
litResults.Text = "Obama: " + oCount + " votes (" + oPercent + "%).<br />";
litResults.Text = litResults.Text + "McCain: " + mCount + " votes (" + mPercent + "%).<br /><br />";
}

protected void butResults_Click(object sender, EventArgs e)
{
readXML();
}
}

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks