This tutorial will demonstrate how to create an online poll to allow users to vote only once using ASP.NET 4.0 and C#.

For this tutorial we will be creating a simple web page that will show the total votes for two topics in a database and allow the user to vote only one time. To do this, we will be creating a database to keep track of how many votes have been counted for each topic and if a user has voted already.

Adding the Default.aspx Page

At this point in the tutorial I have created a new ASP.NET Empty Web Site. Next, we need to add a simple form that will allow a user to login and vote. To do this:

  1. Right click the project in your solution explorer.
  2. Select add new item…
  3. Select a web form.
  4. Name it ‘Default.aspx’.
  5. Click add.
  6. Open Default.aspx up to design mode.
  7. Drag and drop a loginview control onto the web form.
  8. Expand the loginview tasks menu and select AnonymousTemplate from the drop down list.
  9. Drag and drop a login control into the loginview.
  10. Underneath the loginview add in a new table that has 2 rows and 2 columns.
  11. Drag and drop a label into the top left cell of the table.
  12. Change the ID property of the label to ‘lblA’.
  13. Drag and drop a label into the top right cell of the table.
  14. Change the ID property of the label to ‘lblB’.
  15. Drag and drop a button into the bottom left cell of the table.
  16. Change the ID property of the button to ‘btnA’.
  17. Change the Text property of the button to ‘Vote A’.
  18. Drag and drop a button into the bottom right cell of the table.
  19. Change the ID property of the button to ‘btnB’.
  20. Change the Text propert of the button to ‘Vote B’.

You should now have a simple form that looks like this:

Adding the Database

Next, we need to add in a database with a few small tables that will allow us to keep track of the votes. To do this:

  1. Right click the project in your solution explorer.
  2. Select add ASP.NET folder.
  3. Select App_Data.
  4. Right click the App_Data folder.
  5. Select add new item…
  6. Select a SQL Database.
  7. Name it ‘Database.mdf’.
  8. Click add.

Next, we need to add in two tables, one to keep track of the votes and one to keep track of the users who have already voted. To do this:

  1. Expand the Database.mdf folder in your server/database explorer.
  2. Right click the Tables folder.
  3. Select add new table.
  4. Add the following columns with their respective types to the table:
    Column Name Data Type
    VoteIn nvarchar(50)
    NumVotes int
  5. Save the table as ‘Votes’.
  6. Right click the Tables folder.
  7. Select add new table.
  8. Add the following columns with their respective types to the table:
    Column Name Data Type
    UserName nvarchar(50)
  9. Save the table as ‘Voted’.

Adding the ConnectionString

Now that we have our database setup, we need to add a connection string to it. To do this, add in the following code to the Web.Config file in between the and tags:

Enabling and Creating Users

In order for us to vote, we are going to need to be logged in to an account. To enable user creation and create a new user:

  1. Click the ASP.NET Configuration icon in your solution explorer.
  2. In the ASP.NET Web Site Administration Tool click the Security tab.
  3. Click Select authentication type.
  4. Select From the internet.
  5. Click Done.
  6. Click Create User.
  7. Fill out the information and create a new account.
  8. Close the ASP.NET Web Site Administration Tool.

Adding the Voting Functionality

Next, we need to add in some C# code that will allow us to vote if we are logged in and have not yet voted. To avoid incorrect polls we avoid letting users vote more than one time. Here I am going to limit their voting by 1 vote per unique username, however we can just as easily limit this another way by using something like an IP or even a MAC address. To begin, open up Default.aspx.cs up for editing and add the following code to the Page_Load event method:

This code will determine whether or not to enable the voting buttons based on if the user is logged in and has already voted or not. Next, we are going to write a method that will actually submit a Vote given a VoteId passed by a string to it. To do this, add the following method to the Default.aspx.cs class under the Page_Load method:

This will call the Vote method we added earlier, passing the VoteId for option ‘A’ that the user has chosen to vote for. Next, we need to do the same thing for btnB, go back to the Default.aspx and double click btnB and add the following code to the btnB_Click event method:

This will call the Vote method passing the VoteId for option ‘B’.

Testing

To test this out, go ahead and load up the web site. Login to the account that you created earlier and choose to vote for option ‘A’ by clicking btnA. Notice that the page is reloaded and the buttons are now disabled even though you are logged in, this is because your account has already voted. Navigate to the Votes table in your database, right click it, and select Show Table Data. Notice that the number of votes corresponding to option ‘A’ has increased from 39 to 40. This is not the most secure voting system, but it is fairly easy to implement and will limit each account to one vote while keeping an accurate count of all votes.

The Default.aspx source looks like this:

Download Source Files