Online Poll System with percentage results in ASP.NET using VB.NET

In this article you will learn that how you can create an online Poll system with percentage result.
  • 4704
 

Introduction

In this article I am explaining that how you can create an online poll system which makes use of AJAX, XML and provide percentage results. Poll System can be very useful in gathering a large amount of data quickly. They usually require no registration, and are mainly used to gather such information as opinions or trends. Visiters to the website are usually willing to take part in polls because they usually do not require any personal information, and people are interested in other's views. In this voting system anyone can vote and we can see the result of the poll including a percentage of the answer. The system is using XML for storage of votes and AJAX to enable instant result. The implementation of Poll System needs a ScriptManager, UpdatePanel, Literal, RadioButtonList, Label and a Button control.
 

Getting Started

  • Simply create a new ASP.NET web application. 
  • Then right-click on your project in solution explorer than select add new item and select the XML file Template and give name to it. As we have given Poll.xml . 
  • Write the below code in Poll.xml file.

    <?xml version="1.0" encoding="utf-8" ?>
    <
    Poll>
      <
    Vote>
        <
    Name>Paul</Name>
        <
    Choice>Obama</Choice>
      </
    Vote>
      <
    Vote>
        <
    Name>Mike</Name>
        <
    Choice>McCain</Choice>
      </
    Vote>
    </
    Poll>
     
  • Drag  a ScriptManager, UpdatePanel, Literal, RadioButtonList, Label and Button control on your web page. The page will look like below.

    POLL1.gif
     
  • Now your Default.aspx page will look like below.

    <%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeBehind="Default.aspx.vb" Inherits="AJAXDropDownList._Default" %>
    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </
    asp:Content>
    <
    asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">   
    <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>
    </
    asp:Content>
     
  • Then attach the below code in the code behind file of the web page.

       
    Protected Sub butVote_Click(ByVal sender As Object, ByVal e As EventArgs)
           
    If txtName.Text = "" Then
                lblStatus.Text = "Please enter your name."
            ElseIf radVote.SelectedItem Is Nothing Then
                lblStatus.Text = "Please vote."
            Else
                countVote(radVote.SelectedItem.ToString())
           
    End If
        End Sub
        Protected Sub countVote(ByVal theVote As String)
           
    Try
                Dim xmlDoc As XDocument = 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."
            End Try
        End Sub
        Protected Sub readXML()
           
    Dim xmlDoc As XDocument = XDocument.Load(Server.MapPath("Poll.xml"))
           
    Dim votes = From vote In xmlDoc.Descendants("Vote") _
           
    Select Name = vote.Element("Name").Value, Vote = vote.Element("Choice").Value
           
    Dim mCount As Integer = 0
           
    Dim oCount As Integer = 0 
           
    For Each vote In votes
               
    If vote.Vote = "McCain" Then
                    mCount += 1
               
    ElseIf vote.Vote = "Obama" Then
                    oCount += 1
               
    End If
            Next vote
           
    Dim theTotal As Double = mCount + oCount
           
    Dim mPercent As Double = (mCount / theTotal) * 100
           
    Dim oPercent As Double = (oCount / theTotal) * 100
            litResults.Visible =
    True
            litResults.Text = "Obama: " & oCount & " votes (" & oPercent & "%).<br />"
            litResults.Text = litResults.Text & "McCain: " & mCount & " votes (" & mPercent & "%).<br /><br
            />"
        End Sub
        Protected Sub butResults_Click(ByVal sender As Object, ByVal e As EventArgs)
            readXML()
       
    End Sub
     
  • Now run your application.

Output

POLL2.gif

POLL3.gif

POLL4.gif

Summary
 
In this article you learned that how to create a online pool system with percentage result.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.