Tuesday 28 May 2013

Create Online Poll For Your Website Using Asp.Net In C#

Create Online Poll For Your Website Using Asp.Net In C#

so for this article create a new asp.net application and add a form.  But before that create table in your data base.

sql table

Now in your asp.net add the below code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication5.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>Code to Create Online Poll System With Percentage Graphs Using Asp.Net In C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table width="50%">
    <tr>
    <td>
    <table width="100%">
    <tr>
    <td> <asp:Label ID="lblquestion" runat="server" Text="This is a demo poll question?"></asp:Label></td>
    </tr><tr>
    <td><input id="Radio1" type="radio"  runat="server" name="op1"/><asp:Label ID="Label4"  runat="server" Text="Label"></asp:Label><br/>
        <input id="Radio2" type="radio"  runat="server" name="op1"/><asp:Label ID="Label5"  runat="server" Text="Label"></asp:Label><br/>
        <input id="Radio3" type="radio"  runat="server" name="op1"/><asp:Label ID="Label6"  runat="server" Text="Label"></asp:Label><br/>
</td>
       
    </tr>
    </table>
       <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Submit" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"
            style="color: #FF3300; font-weight: 700; font-size: large"></asp:Label>
                <asp:Label ID="Label3" runat="server"
            style="color: #FF3300; font-weight: 700; font-size: large;"></asp:Label>
    </td>
    </tr>
    <tr>
    <td> 
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            EmptyDataText="There are no data records to display." Width="100%"
            CellPadding="2" CellSpacing="2" onrowdatabound="GridView1_RowDataBound">
            <Columns>             
                <asp:TemplateField HeaderText="Option" SortExpression="Option">                
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text=`<%# Bind("PollOption") %>`></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="OptionValue" HeaderText="Option Value"/>
                 <asp:TemplateField HeaderText="Graph" SortExpression="Graph">              
                    <ItemTemplate>
                     <div id="divvoteper" runat="server" style="background:red;height:5px;"></div>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle BackColor="#99CCFF" />
        </asp:GridView>      
   
        <br />
     </td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>

Now add the below code in your .cs file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.HtmlControls;
namespace WebApplication5
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        DataTable objdtpoll = new DataTable();
        protected void Button1_Click(object sender, EventArgs e)
        {
            string optionID = "0";
            if (Radio1.Checked == true)
            {
                optionID = "1";
            }
            else if (Radio2.Checked == true)
            {
                optionID = "2";
            }
            else if (Radio3.Checked == true)
            {
                optionID = "3";
            }
            SqlConnection _objcon = new SqlConnection("Data Source=VINAY-809553B1F\\TSS;Initial Catalog=DEMO;Integrated Security=True");
            string query = "Update [OnlinePole] set OptionValue=OptionValue+1  where ID=" + optionID;
            SqlDataAdapter objda = new SqlDataAdapter(query, _objcon);
            objda.SelectCommand.CommandType = CommandType.Text;
            _objcon.Open();
            objda.SelectCommand.ExecuteNonQuery();
            _objcon.Close();
            BindGrid();
            Label1.Text = "Thanks for polling..";
        }
        public void BindGrid()
        {
            SqlConnection _objcon = new SqlConnection("Data Source=VINAY-809553B1F\\TSS;Initial Catalog=DEMO;Integrated Security=True");

            string query = "SELECT [ID],[PollOption],[OptionValue] FROM [OnlinePole]";
            SqlDataAdapter objda = new SqlDataAdapter(query, _objcon);
            _objcon.Open();
            objda.Fill(objdtpoll);
            if (objdtpoll.Rows.Count > 0)
            {
                GridView1.DataSource = objdtpoll;
                GridView1.DataBind();
                //for binding the control value
                for (int i = 0; i < objdtpoll.Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        Radio1.Value = objdtpoll.Rows[i]["ID"].ToString();
                        Label4.Text = objdtpoll.Rows[i]["PollOption"].ToString();
                    }
                    else if (i == 1)
                    {
                        Radio2.Value = objdtpoll.Rows[i]["ID"].ToString();
                        Label5.Text = objdtpoll.Rows[i]["PollOption"].ToString();
                    }
                    else if (i == 2)
                    {
                        Radio3.Value = objdtpoll.Rows[i]["ID"].ToString();
                        Label6.Text = objdtpoll.Rows[i]["PollOption"].ToString();
                    }
                }
            }
            _objcon.Close();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int total = objdtpoll.AsEnumerable().Sum(x => x.Field<int>("OptionValue"));
            HtmlGenericControl div = e.Row.FindControl("divvoteper") as HtmlGenericControl;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                {
                    if (total != 0)
                    {
                        int calculateprecentage = Convert.ToInt32(e.Row.Cells[1].Text) * 100 / total;
                        div.Style["width"] = calculateprecentage.ToString()+"px";
                        div.Style["background-color"] = "green";
                    }
                }
            }
        }
    }
}


Below code is the used for creating the graph.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int total = objdtpoll.AsEnumerable().Sum(x => x.Field<int>("OptionValue"));
            HtmlGenericControl div = e.Row.FindControl("divvoteper") as HtmlGenericControl;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                {
                    if (total != 0)
                    {
                        int calculateprecentage = Convert.ToInt32(e.Row.Cells[1].Text) * 100 / total;
                        div.Style["width"] = calculateprecentage.ToString()+"px";
                        div.Style["background-color"] = "green";
                    }
                }
            }
        }


Now we will run the page.

online poll

Now select option and press submit.

online poll

Thanks....

1 comment:

  1. Gosh, this is so complicated! I prefer SoGoSurvey. It's simple and effective!

    ReplyDelete