Voting Control Application Using ASP.Net C#

Background

In today's increasing technology demands, many tools have come onto the market and there have been reviews of specific products (or other specific things) from customers to improve the product quality or to get opinions of them, so we will make a voting control application using ASP.Net C#.
Use the following procedure to create the sample application "Empsys":
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New Project" - "C#" - "Empty Web Application" (to avoid adding a master page).
  3. Provide the web site a name such as "VotingApp" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer - "Add New Item" - "Default.aspx page".
  5. Drag and drop two buttons, three labels and one Radio Button list
The Default.aspx page source will be as follows:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>    
  2.     
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">    
  4. <html xmlns="http://www.w3.org/1999/xhtml">    
  5. <head runat="server">    
  6.     <title>article by vithal wadje</title>    
  7. </head>    
  8. <body bgcolor="black" style="color: #808000; margin-left: 30px; width: 475px;">    
  9.     <form id="form1" runat="server">    
  10.     <br />    
  11.     <div>    
  12.         <p style="font-family: 'Times New Roman', Times, serif; font-size: large; color: #808000;    
  13.             font-weight: bold; position: inherit">    
  14.             Is Vithal Wadje is Next MLA from Latur ?</p>    
  15.         <asp:RadioButtonList ID="RadioButtonList1" runat="server">    
  16.             <asp:ListItem>Yes</asp:ListItem>    
  17.         </asp:RadioButtonList>    
  18.         <asp:RadioButtonList ID="RadioButtonList2" runat="server" Font-Bold="True">    
  19.             <asp:ListItem>No</asp:ListItem>    
  20.         </asp:RadioButtonList>    
  21.         <br />    
  22.         <asp:Button ID="Button1" runat="server" OnClick="Vote_Click" Text="Vote" />    
  23.               
  24.         <asp:Button ID="Button2" runat="server" Text="View Votes" OnClick="ViewVotes_Click" />    
  25.         <br />    
  26.         <br />    
  27.         <br />    
  28.         <asp:Label ID="Label1" runat="server" Text="Label" Font-Bold="True" Font-Size="X-Large"></asp:Label><br />    
  29.         <asp:Label ID="Label2" runat="server" Text="Label" Font-Bold="True" Font-Size="X-Large"></asp:Label><br />    
  30.         <asp:Label ID="Label3" runat="server" Text="Label" Font-Bold="True" Font-Size="X-Large"></asp:Label>    
  31.     </div>    
  32.     </form>    
  33. </body>    
  34. </html>  
Now the UI will will look such as follows.
 
 
 
In the preceding sample UI I have added one question (it's just an example; I love coding, not MLA post) and on that there are two buttons of a Radio Button List that are "Yes" and "No" and after checking one of the Radio Buttons and clicking on the above Vote button, the vote will be registered and then on the View Votes button click it shows the results of all votes in one label, so let us start coding to fulfill our application requirements.
 
Vote Button click Logic 
  1. protected void Vote_Click(object sender, EventArgs e)    
  2. {    
  3.     if (Request.Cookies["State"] == null//check if cookies is null,in this we checking the user is already voted or not    
  4.     {  
  5.         foreach (ListItem item in RadioButtonList1.Items)    
  6.         {  
  7.             if (item.Selected == true)    
  8.             {    
  9.                 //in C:\\Users\\vithal\\Desktop\\vote\\Result.txt location  Votes are Saved Instead of Database    
  10.                 FileStream fs1 = new FileStream("C:\\Users\\vithal\\Desktop\\vote\\Result.txt", FileMode.Append, FileAccess.Write);    
  11.                 StreamWriter sw1 = new StreamWriter(fs1);    
  12.                 sw1.WriteLine(item.Value);    
  13.                 sw1.Flush();    
  14.                 sw1.Close();    
  15.                 sw1.Close();    
  16.                 HttpCookie HC = new HttpCookie("State");    
  17.                 HC.Values["State"] = "Set";    
  18.                 HC.Expires = DateTime.Now.AddDays(2); //Added cookies Expires time    
  19.                 Response.Cookies.Add(HC);    
  20.                 Label3.Visible = true;    
  21.                 Label3.ForeColor = Color.Green;    
  22.                 Label3.Text = "You Have voted Sucessfully";  
  23.             }   
  24.         }  
  25.     }    
  26.     else    
  27.     {    
  28.         Label3.Visible = true;    
  29.         Label3.ForeColor = Color.Red;    
  30.         Label3.Text = "You are already Voted"//if cookies not null means user is already voted  
  31.     }    
  32. } 
In the code above, when the vote button is clicked, it first checks the cookies for null, if null then it is a successful vote, if the cookies are not null then that means the user has already voted.
 
Note

The Important point of this application is that we are not using a database to store the votes, we are using a text file for that so you need to create the same text file that I created, a Result text file at the location:
 
C:\\Users\\vithal\\Desktop\\vote\\Result.txt 
 
I hope you have created the same. Now to move towards the view Votes button code logic is as in the following:
  1. protected void ViewVotes_Click(object sender, EventArgs e)    
  2. {    
  3.     int yes = 0;    
  4.     int no = 0;    
  5.     FileStream fs2 = new FileStream("C:\\Users\\vithal\\Desktop\\vote\\Result.txt", FileMode.Open, FileAccess.Read);    
  6.     StreamReader sr2 = new StreamReader(fs2);    
  7.   
  8.     sr2.BaseStream.Seek(0, SeekOrigin.Begin);    
  9.     string str = sr2.ReadLine();    
  10.     while (str != null)    
  11.     {    
  12.   
  13.         if (str == "Yes")    
  14.         {    
  15.             yes = yes + 1;    
  16.   
  17.         }    
  18.         if (str == "No")    
  19.         {    
  20.             no = no + 1;    
  21.   
  22.         }    
  23.         str = sr2.ReadLine();   
  24.     }    
  25.     sr2.Close();    
  26.     fs2.Close();    
  27.     float a=(float)yes/(yes+no)*100;   
  28.     int aresult = (int)a;    
  29.     int bresult = 100 - aresult;    
  30.     Label1.Visible = true;    
  31.     Label1.ForeColor = Color.Brown;    
  32.     Label1.Text ="Yes :"+"   "+"  "+ Convert.ToString(aresult) + " " + "%";    
  33.     Label2.Visible = true;    
  34.     Label2.ForeColor = Color.Brown;    
  35.     Label2.Text = "No :"+"   "+"  "+Convert.ToString(bresult) + " " + "%";    
  36.   
  37. } 
In the code above, we are reading the results from a text file using StreamReader, then in one float variable we are counting the percentage  of votes and showing that on a label.
 
We are done with the code, now it's time to run the application and Vote. Now run the application to vote for Vithal Wadje as in the following:
 
 
 
Now, suppose four users have voted, out of the 4, three have voted yes and one of them is No and I have voted from my browser, then if I try to vote again, it shows the following message:
 
 
 
In other words, I have already voted and using cookies we are detecting duplicate votes. For further explanations please see the code.
 
Now click on the View votes button, the result will be as in the following:
 
 
 
Now in the preceding result, you can see that 96 % of the people voted yes Vithal Wadje is the next MLA from Latur and 4% are unhappy peoples saying No Vithal Wadje is not the next MLA from Latur, in other words before election, I need to make them happy!
 
Now from all the preceding examples, we have learned how to create a voting application. I hope you have enjoyed this article as you read it.
Note:
  • For detailed code please download the Zip file attached above.
  • Don't forget to create the text file for storing votes.
Summary

Now from all the preceding examples, we have learned how to create a voting application in ASP.Net C# and depending on your requirements you can change the logic. I hope you have enjoyed reading this article. If you have any suggestion regarding this article then please contact me.


Similar Articles