Click here to Skip to main content
11,796,393 members (71,521 online)
Click here to Skip to main content

Webbased Voting Or Survey Application

, 18 Aug 2010 CPOL 13.8K 12
Rate this:
Please Sign up or sign in to vote.
Application receives votes from different computer with validation of time limit and displays the same


Hello everyone... thank you for giving your time to read this article.

This application simply counts and calculates votes gathered from different clients and finally displays them.

Here, I have kept validation that one user can only vote one time in one Hour of Timespan. Although I have not used any authentication of user, it simply detects IP of voter and stores them in XML file and keeps validating.

Application stores data to XML file rather than database for Quick propagation and flexibility of data, and giving high performance on round trips.


Frankly speaking, I made this application for my use only... there is an interesting reason behind it.

Indian idol season 5 (an Indian Singing competition) was going on those days and there were three finalists and I was very curious to know what my friends think who will finally win the season. FYI, there were three finalists on the floor at that time.

Nothing much interesting about this application. I simply developed it to sharpen my skills in extra time. And I thought someone may find it useful or may be get a path to their application.

Using the Code

Ahh.. The main thing is code.

To store data in XML, I have specified the format stated below.

<?xml version="1.0" encoding="utf-8"?>
  <Participant Name="Sreeram Chandra" ID="SRC" Vote="22">
    <Vote IP="" DateTime="8/14/2010 2:57:39 PM" HostName="" />
  <Participant Name="Bhoomi Trivedi" ID="BT" Vote="23">
    <Vote IP="" DateTime="8/12/2010 2:59:20 PM" HostName="" />
    <Vote IP="" DateTime="8/13/2010 1:57:14 PM" HostName="" />
    <Vote IP="" DateTime="8/14/2010 1:06:02 PM" HostName="" />
  <Participant Name="Rakesh Maini" ID="RM" Vote="21">
    <Vote IP="" DateTime="8/15/2010 12:08:59 PM" HostName="" />
    <Vote IP="" DateTime="8/16/2010 1:08:00 PM" HostName="" />

Let's dig into the above XML format. In that, you will find root node which depicts participants, I mean the people or things for which polling is to be done and under that node, there is another node called Vote which is the sub-node of Participants which is Vote given by an individual user so that we can keep history of vote and can store IP of voters.

public class VoteParticipant
    public string Name { get; set; }
    public List<voters> VotersList { get; set; }
    public string ID { get; set; }
    public int Vote { get; set; }
    public static void insertVotetoXML(Voters vote,string FileName)
        String VoteXMLPath = HttpContext.Current.Server.MapPath(FileName);
        XmlDocument XmlDoc = new XmlDocument();
        XmlNodeList nodeList = XmlDoc.GetElementsByTagName("Participant");
        foreach (XmlNode node in nodeList)
            if (node.Attributes["ID"].Value == vote.ID)
                XmlNode newNode = XmlDoc.CreateNode(XmlNodeType.Element, "Vote", "");
                newNode.Attributes["IP"].Value = vote.IP;
                newNode.Attributes["DateTime"].Value = vote.VoteDateTime.ToString();
                newNode.Attributes["HostName"].Value = vote.HostName;
                node.Attributes["Vote"].Value = 
		(int.Parse(node.Attributes["Vote"].Value) + 1).ToString();
        SetVoteQuote(FileName, vote);

    private static void SetVoteQuote(string FileName,Voters vote)
        List<poll> pollList = PollGenerator.returnPolls().Where
			(p => p.XmlFileName == FileName).ToList();
        List<voteparticipant> participantList = VoteParticipant.GetParticipants
        QuoteService.Gotten.Add(new VoteQuote() 
		{ DateTime = vote.VoteDateTime, FromIP = vote.IP, 
		ParticipantName = participantList[0].Name, 
		PollName = pollList[0].Title });
    public static List<voteparticipant> GetParticipants(string FileName)
        String VoteXMLPath = HttpContext.Current.Server.MapPath(FileName);
        XmlDocument XmlDoc = new XmlDocument();
        XmlNodeList nodeList = XmlDoc.GetElementsByTagName("Participant");
        List<voteparticipant> iList = new List<voteparticipant>();
        foreach (XmlNode node in nodeList)
            VoteParticipant objParticipant = new VoteParticipant();
            objParticipant.ID = node.Attributes["ID"].Value.ToString();
            objParticipant.Name = node.Attributes["Name"].Value.ToString();
            objParticipant.Vote = int.Parse(node.Attributes["Vote"].Value.ToString());
            List<voters> objVoters = new List<voters>();
            if (node.HasChildNodes)
                XmlNodeList childNodes = node.ChildNodes;
                foreach (XmlNode childNode in childNodes) 
                    objVoters.Add(new Voters 
			{ ID = node.Attributes["ID"].Value, 
			IP = childNode.Attributes["IP"].Value, 
			VoteDateTime = DateTime.Parse(childNode.Attributes
			HostName = childNode.Attributes["HostName"].Value });
            objParticipant.VotersList = objVoters;
        return iList;
public class Voters
    public string ID { get; set; }
    public string IP { get; set; }
    public DateTime VoteDateTime { get; set; }
    public string HostName { get; set; }
    public Voters() { }

The above C# code depicts the following things:

Class VoteParticipant

    public static void insertVotetoXML(Voters vote,string FileName)

    public static List<voteparticipant /> GetParticipants(string FileName)

Class Voters

Above, there are two classes: one is voteparticipant which has two methods to insert newly created vote to XML and another is to get whole XML in a Votepartcipant List format to get all the participants and all the voters belonging to that participant.

Now take a look at the ASPX page structure:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="">
<head runat="server">
    <title>Vote your Vote</title>
    <form id="form1" runat="server">
    <div id="Header">
        <span id="spnTitle" runat="server" style='font-weight:15px;
        <asp:Label ID="lblTitle" runat="server"></asp:Label>
    <div id="divVote" runat="server">
        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        <asp:Button ID="btnVote" runat="server" Text="VOTE" OnClick="btnVote_Click" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ErrorMessage="Select one of Participant First"></asp:RequiredFieldValidator>
    <div id="divResult" runat="server">
        <asp:GridView ID="grdResult" runat="server" onrowcreated="grdResult_RowCreated">

And also look at the Back End of the Form:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Vote : System.Web.UI.Page
    string CurrentxmlFileName = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
        CurrentxmlFileName = Server.MapPath("VoteXML.xml");
        if (!Page.IsPostBack)
            lblTitle.Text = PollGenerator.GetTitle(CurrentxmlFileName);
            bool Flag = true;
            List<voteparticipant> objParticipants = 
            foreach (VoteParticipant participan in objParticipants)
                List<voters> voters = participan.VotersList;
                foreach (Voters vote in voters)
                    if (vote.IP == Request.ServerVariables["REMOTE_ADDR"].ToString())
                        DateTime VoteTime = vote.VoteDateTime;
                        TimeSpan ts = DateTime.Now.Subtract(VoteTime);
                        if (!(ts.Hours >= 1))
                            Flag = false;
            if (Flag)
                List<voteparticipant> SettoRadioButton = VoteParticipant.GetParticipants
                foreach (VoteParticipant participant in SettoRadioButton)
                    RadioButtonList1.Items.Add(new ListItem
				(participant.Name, participant.ID));
    protected void btnVote_Click(object sender, EventArgs e)
        Voters vote = new Voters();
        vote.HostName = Request.ServerVariables["REMOTE_HOST"].ToString();
        vote.ID = RadioButtonList1.SelectedItem.Value;
        vote.IP = Request.ServerVariables["REMOTE_ADDR"].ToString();
        vote.VoteDateTime = DateTime.Now;
        VoteParticipant.insertVotetoXML(vote, CurrentxmlFileName);

    private void PopulateResult()
        DataTable Table = new DataTable();
        Table.Columns.Add("Real Vote");
        int TotalVote = 0;
        List<voteparticipant> GetResult = 
        foreach (VoteParticipant participant in GetResult)
            TotalVote = TotalVote + participant.Vote;
        foreach (VoteParticipant participant in GetResult)
            string Name = participant.Name;
            int PerVote = 0;
            if (TotalVote > 0)
                PerVote = (participant.Vote * 100) / TotalVote;
            int PartVote = participant.Vote;

            Table.Rows.Add(new string[3] { Name, PerVote.ToString() + 
					"%", PartVote.ToString() });
        grdResult.DataSource = Table;
    protected void ShowResult()
        Page.FindControl("divVote").Visible = false;
        Page.FindControl("divResult").Visible = true;
    protected void ShowVote()
        Page.FindControl("divVote").Visible = true;
        Page.FindControl("divResult").Visible = false;
    protected void grdResult_RowCreated(object sender, GridViewRowEventArgs e)
        string onMouseOverStyle = "'blue';'white';";
        string onMouseOutStyle = "'@backgroundColor';'@ForeColor'";
        string RowBackColor = String.Empty;
        if (e.Row.RowType == DataControlRowType.DataRow)
            e.Row.Attributes.Add("onmouseover", onMouseOverStyle);
            e.Row.Attributes.Add("onmouseout", onMouseOutStyle.Replace
		("@backgroundColor", "White").Replace("@ForeColor", "black"));

Nothing new in the above code, it just checks on the page Load event that if the user(dedicated IP) has already voted in one hour of timespan, then it will instantly show them a Result division directly, and if not, then it takes to Voting Division.

Points of Interest

Just for fun!!!



  • 18th August, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hiren solanki
Software Developer
India India
He is a Smart IT devloper with Few years of Expeariance But having Great command on,C#,SQL Query,SSRS,Crystal Reports

Apart from that He Loves multimedia work too, Master of Adobe photoshop, Illustrator, CSS , HTML and all things.

He is Currently working in Microsoft Dynamics CRM and Having Nice Expearince with CRM. CRM Rocks!!!

You may also be interested in...

Comments and Discussions

BugIncomplete code Pin
adeel198123-Mar-12 20:05
memberadeel198123-Mar-12 20:05 
QuestionAwesome! Pin
Member 867945726-Feb-12 12:57
memberMember 867945726-Feb-12 12:57 
GeneralMy vote of 3 Pin
Uma Shankar Patel14-Dec-11 19:23
memberUma Shankar Patel14-Dec-11 19:23 
Generalsome classe does not in this code Pin
Uma Shankar Patel14-Dec-11 19:23
memberUma Shankar Patel14-Dec-11 19:23 
GeneralMy vote of 1 Pin
dineshkummarc23-Mar-11 0:09
memberdineshkummarc23-Mar-11 0:09 
GeneralMy vote of 5 Pin
KunalChowdhury26-Sep-10 22:36
mentorKunalChowdhury26-Sep-10 22:36 
GeneralRe: My vote of 5 Pin
Hiren Solanki27-Sep-10 20:46
memberHiren Solanki27-Sep-10 20:46 
Generalgood work Pin
Sandeep Ramani18-Aug-10 19:54
memberSandeep Ramani18-Aug-10 19:54 
GeneralRe: good work [modified] Pin
Hiren Solanki18-Aug-10 22:40
memberHiren Solanki18-Aug-10 22:40 
GeneralMy vote of 3 Pin
Sandeep Ramani18-Aug-10 19:53
memberSandeep Ramani18-Aug-10 19:53 
GeneralRe: My vote of 3 [modified] Pin
Hiren Solanki18-Aug-10 22:40
memberHiren Solanki18-Aug-10 22:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 18 Aug 2010
Article Copyright 2010 by Hiren solanki
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid