protected void Page_Load(object sender, EventArgs e)
{
}
protected void butVote_Click(object sender, EventArgs e)
{
if (radVote.SelectedItem == null)
lblStatus.Text = "Please vote.";
else
countVote(radVote.SelectedItem.ToString());
}
protected void countVote(string theVote)
{
try
{
string conn = ConfigurationManager.ConnectionStrings["PollConnectionString"].ToString();
PollDataContext context = new PollDataContext(conn);
tblPoll pt = new tblPoll();
pt.vote = theVote;
context.tblPolls.InsertOnSubmit(pt);
context.SubmitChanges();
lblStatus.Text = "Thank you for your vote.";
readData();
}
catch
{
lblStatus.Text = "Sorry, unable to process request. Please try again.";
}
}
protected void readData()
{
string conn = ConfigurationManager.ConnectionStrings["PollConnectionString"].ToString();
PollDataContext context = new PollDataContext(conn);
tblPoll pt = new tblPoll();
var votes = from vote in context.tblPolls select vote;
int mCount;
int oCount;
mCount = 0;
oCount = 0;
foreach (var vote in votes)
{
if (vote.vote == "McCain")
mCount++;
else if (vote.vote == "Obama")
oCount++;
}
double theTotal;
theTotal = mCount + oCount;
double mPercent;
double oPercent;
mPercent = (mCount/theTotal)*100;
oPercent = (oCount/theTotal)*100;
litResults.Visible = true;
litResults.Text = "Obama: " + oCount + " votes (" + oPercent + "%).<br />";
litResults.Text = litResults.Text + "McCain: " + mCount + " votes (" + mPercent + "%).<br /><br />";
}