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. VB.NET

Poll systems can be very useful for gathering a large amount of data quickly. They usually require no registration, and are mainly used to gather such information as opinions or trends. Visitors to your 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 tutorial, you will learn how to 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 use XML for storage of votes, and AJAX to enable instant results. We will start by creating the structure of the XML file:

Next, we can start on the ASPX page. Add in the ScriptManager and UpdatePanel tags:

[thml]




[/html]

Now we can build our Poll form inside the UpdatePanel, so that AJAX can reload it without performing a full PostBack.
We will be using a TextBox, RadioButtons, Buttons, and a Label and Literal control. Our ASPX page will look something like this:

Notice we have two buttons on our page – one to submit the vote, and the other to show the current results.
First, we will code the submit button. We want this button to check that a name has been entered and that an option has been selected:

The submit button doesn’t perform anything but validation, we then call a method to submit the vote to the XML file:

This method uses LINQ to first open the XML file and then add a new element to it with the values the user submitted. We then save the updated XML file and output the current results to the page, by calling the readXML method:

Again, this method makes use of LINQ to open the XML file and get all data within. It then loops through all the data and counts the number of votes for each candidate, then performs a simple calculation to determine the percentage for each. Finally, we output the results to the web page, using the Literal control.
We also code the show results button to call the readXML method:

Download Source Files