Introduction
Here I will explain how to make basic voting app in ASP.Net C#.
I will use cookies to identify unique user . (Note: User can vote again by clearing cookies.).
Reference: unknown.

Store Vote
I am using a text file to store user votes . You have to just crate a text file @"C/temp/Result.txt" .
Home.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Simple Voting</title>
</head>
<body>
<form id="form1" runat="server">
<br />
<div>
<table>
<tr>
<td colspan="3"><b>Is currency demonetization good or bad?</b></td>
</tr>
<tr>
<td colspan="3">
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
<asp:ListItem Value="NoIdea">No Comment</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<asp:Button ID="btnVote" runat="server" OnClick="btnVote_Click" Text="Vote" />
<asp:Button ID="btnView" runat="server" Text="View Result" OnClick="btnView_Click" />
</td>
</tr>
<tr>
<td>Result:
</td>
<td width="10px"></td>
<td>
<label>
<asp:Label ID="lblMsg1" runat="server" Text="Label" Font-Bold="True"></asp:Label><br />
<asp:Label ID="lblMsg2" runat="server" Text="Label" Font-Bold="True"></asp:Label><br />
<asp:Label ID="lblMsg3" runat="server" Text="Label" Font-Bold="True"></asp:Label><br />
<asp:Label ID="lblMsg4" runat="server" Text="Label" Font-Bold="True"></asp:Label>
</label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Home.aspx.cs (Code Behind)
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblMsg1.Visible = false;
lblMsg2.Visible = false;
lblMsg3.Visible = false;
lblMsg4.Visible = false;
}
protected void btnVote_Click(object sender, EventArgs e)
{
if (Request.Cookies["State"] == null)
{
foreach (ListItem item in RadioButtonList1.Items)
{
if (item.Selected == true)
{
FileStream fs1 = new FileStream("C:\\temp\\Result.txt", FileMode.Append, FileAccess.Write);//Physical file path
StreamWriter sw1 = new StreamWriter(fs1);
sw1.WriteLine(item.Value);
sw1.Flush();
sw1.Close();
sw1.Close();
HttpCookie HC = new HttpCookie("State");
HC.Values["State"] = "Set";
HC.Expires = DateTime.Now.AddDays(2); //Added cookies Expires time
Response.Cookies.Add(HC);
lblMsg4.Visible = true;
lblMsg4.ForeColor = Color.Green;
lblMsg4.Text = "You Have voted Sucessfully";
}
}
}
else
{
lblMsg4.Visible = true;
lblMsg4.ForeColor = Color.Red;
lblMsg4.Text = "You are already Voted";
}
}
protected void btnView_Click(object sender, EventArgs e)
{
int yes = 0;
int no = 0;
int noIdea = 0;
FileStream fs2 = new FileStream("C:\\temp\\Result.txt", FileMode.Open, FileAccess.Read);
StreamReader sr2 = new StreamReader(fs2);
sr2.BaseStream.Seek(0, SeekOrigin.Begin);
string str = sr2.ReadLine();
while (str != null)
{
if (str == "Yes")
{
yes = yes + 1;
}
if (str == "No")
{
no = no + 1;
}
if (str == "NoIdea")
{
noIdea = noIdea + 1;
}
str = sr2.ReadLine();
}
sr2.Close();
fs2.Close();
float a = (float)yes / (yes + no+noIdea) * 100;
float b = (float)no / (yes + no + noIdea) * 100;
float c = (float)noIdea / (yes + no + noIdea) * 100;
int aresult = (int)a;
int bresult = (int)b;
int cresult =100 - (aresult+bresult);
lblMsg1.Visible = true;
lblMsg1.ForeColor = Color.Brown;
lblMsg1.Text = "Yes :" + " " + " " + Convert.ToString(aresult) + " " + "%";
lblMsg2.Visible = true;
lblMsg2.ForeColor = Color.Brown;
lblMsg2.Text = "No :" + " " + " " + Convert.ToString(bresult) + " " + "%";
lblMsg3.Visible = true;
lblMsg3.ForeColor = Color.Brown;
lblMsg3.Text = "No Comment :" + " " + " " + Convert.ToString(cresult) + " " + "%";
}
}
Here I will explain how to make basic voting app in ASP.Net C#.
I will use cookies to identify unique user . (Note: User can vote again by clearing cookies.).
Reference: unknown.

Store Vote
I am using a text file to store user votes . You have to just crate a text file @"C/temp/Result.txt" .
Home.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Simple Voting</title>
</head>
<body>
<form id="form1" runat="server">
<br />
<div>
<table>
<tr>
<td colspan="3"><b>Is currency demonetization good or bad?</b></td>
</tr>
<tr>
<td colspan="3">
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
<asp:ListItem Value="NoIdea">No Comment</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<asp:Button ID="btnVote" runat="server" OnClick="btnVote_Click" Text="Vote" />
<asp:Button ID="btnView" runat="server" Text="View Result" OnClick="btnView_Click" />
</td>
</tr>
<tr>
<td>Result:
</td>
<td width="10px"></td>
<td>
<label>
<asp:Label ID="lblMsg1" runat="server" Text="Label" Font-Bold="True"></asp:Label><br />
<asp:Label ID="lblMsg2" runat="server" Text="Label" Font-Bold="True"></asp:Label><br />
<asp:Label ID="lblMsg3" runat="server" Text="Label" Font-Bold="True"></asp:Label><br />
<asp:Label ID="lblMsg4" runat="server" Text="Label" Font-Bold="True"></asp:Label>
</label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Home.aspx.cs (Code Behind)
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblMsg1.Visible = false;
lblMsg2.Visible = false;
lblMsg3.Visible = false;
lblMsg4.Visible = false;
}
protected void btnVote_Click(object sender, EventArgs e)
{
if (Request.Cookies["State"] == null)
{
foreach (ListItem item in RadioButtonList1.Items)
{
if (item.Selected == true)
{
FileStream fs1 = new FileStream("C:\\temp\\Result.txt", FileMode.Append, FileAccess.Write);//Physical file path
StreamWriter sw1 = new StreamWriter(fs1);
sw1.WriteLine(item.Value);
sw1.Flush();
sw1.Close();
sw1.Close();
HttpCookie HC = new HttpCookie("State");
HC.Values["State"] = "Set";
HC.Expires = DateTime.Now.AddDays(2); //Added cookies Expires time
Response.Cookies.Add(HC);
lblMsg4.Visible = true;
lblMsg4.ForeColor = Color.Green;
lblMsg4.Text = "You Have voted Sucessfully";
}
}
}
else
{
lblMsg4.Visible = true;
lblMsg4.ForeColor = Color.Red;
lblMsg4.Text = "You are already Voted";
}
}
protected void btnView_Click(object sender, EventArgs e)
{
int yes = 0;
int no = 0;
int noIdea = 0;
FileStream fs2 = new FileStream("C:\\temp\\Result.txt", FileMode.Open, FileAccess.Read);
StreamReader sr2 = new StreamReader(fs2);
sr2.BaseStream.Seek(0, SeekOrigin.Begin);
string str = sr2.ReadLine();
while (str != null)
{
if (str == "Yes")
{
yes = yes + 1;
}
if (str == "No")
{
no = no + 1;
}
if (str == "NoIdea")
{
noIdea = noIdea + 1;
}
str = sr2.ReadLine();
}
sr2.Close();
fs2.Close();
float a = (float)yes / (yes + no+noIdea) * 100;
float b = (float)no / (yes + no + noIdea) * 100;
float c = (float)noIdea / (yes + no + noIdea) * 100;
int aresult = (int)a;
int bresult = (int)b;
int cresult =100 - (aresult+bresult);
lblMsg1.Visible = true;
lblMsg1.ForeColor = Color.Brown;
lblMsg1.Text = "Yes :" + " " + " " + Convert.ToString(aresult) + " " + "%";
lblMsg2.Visible = true;
lblMsg2.ForeColor = Color.Brown;
lblMsg2.Text = "No :" + " " + " " + Convert.ToString(bresult) + " " + "%";
lblMsg3.Visible = true;
lblMsg3.ForeColor = Color.Brown;
lblMsg3.Text = "No Comment :" + " " + " " + Convert.ToString(cresult) + " " + "%";
}
}
If you will do vote again then ..
View Result
No comments:
Post a Comment