Login page:
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th>
UserName
</th>
<th>
Password
</th>
</tr>
<tr>
<td>
<asp:TextBox ID="txtUserName" runat="server" Text="azim" />
</td>
<td>
<asp:TextBox ID="txtPassword" TextMode="Password" Text="pass" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" Text="Login" runat="server" OnClick="UserLogin" />
</td>
</tr>
</table>
</div>
</form>
C#:
protected void UserLogin(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection _cn = new SqlConnection(constr))
{
using (SqlCommand _cmd = new SqlCommand("SELECT * FROM Users WHERE UserName = @UserName and Password = @Password", _cn))
{
using (SqlDataAdapter da = new SqlDataAdapter(_cmd))
{
_cmd.Parameters.AddWithValue("@UserName", this.txtUserName.Text);
_cmd.Parameters.AddWithValue("@Password", this.txtPassword.Text);
_cn.Open();
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
Session["UserId"] = ds.Tables[0].Rows[0]["UserId"].ToString();
Response.Redirect("Default.aspx");
}
_cn.Close();
}
}
}
}
Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script src="Script/jquery_1.8.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#Button1').on('click', function (e) {
var main = $('#Ratingdiv');
main.each(function () {
var value = $(main).find('.star:checked').val();
InsertVote(value)
});
});
});
</script>
<script type="text/javascript">
function InsertVote(value) {
$.ajax({
type: "POST",
url: "Default.aspx/SaveVote",
data: '{vote: "' + value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
location.reload();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Average vote till now
<div id="Ratingdiv">
<label id="lblOne" runat="server" for="rbtn1">
<input id="rbtn1" type="radio" class="star" name="dynamic-control" value="1" />
One</label>
<label id="lblTwo" runat="server" for="rbtn2">
<input id="rbtn2" type="radio" class="star" name="dynamic-control" value="2" />
Two</label>
<label id="lblThree" runat="server" for="rbtn3">
<input id="rbtn3" type="radio" class="star" name="dynamic-control" value="3" />
Three</label>
<label id="lblFour" runat="server" for="rbtn4">
<input id="rbtn4" type="radio" class="star" name="dynamic-control" value="4" />
Four</label>
<label id="lblFive" runat="server" for="rbtn5">
<input id="rbtn5" type="radio" class="star" name="dynamic-control" value="5" />
Five</label>
</div>
<div style="padding: 20px">
<div style="padding: 5px; clear: both;">
<input id="Button1" type="button" value="Vote" />
</div>
</div>
</div>
</form>
</body>
</html>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateAverageVote();
}
}
private void PopulateAverageVote()
{
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT AVG(Vote) as Vote FROM VoteRating", con))
{
con.Open();
object value = cmd.ExecuteScalar();
int voteValue = value == DBNull.Value ? 0 : Convert.ToInt32(value);
con.Close();
this.lblOne.Style["color"] = voteValue >= 1 ? "red" : "black";
this.lblTwo.Style["color"] = voteValue >= 2 ? "red" : "black";
this.lblThree.Style["color"] = voteValue >= 3 ? "red" : "black";
this.lblFour.Style["color"] = voteValue >= 4 ? "red" : "black";
this.lblFive.Style["color"] = voteValue >= 5 ? "red" : "black";
}
}
}
[WebMethod(EnableSession = true)]
[ScriptMethod]
public static string SaveVote(string vote)
{
int userId = Convert.ToInt32(HttpContext.Current.Session["UserId"]);
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(userId) FROM VoteRating WHERE UserId = @UserId", con))
{
cmd.Parameters.AddWithValue("@UserId", userId);
con.Open();
int isAvailable = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
if (isAvailable < 1)
{
Insert(vote, userId);
return string.Format("You voted {0}", vote);
}
else
{
return "Already voted";
}
}
}
}
private static void Insert(string vote, int userId)
{
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con2 = new SqlConnection(constr))
{
using (SqlCommand cmd2 = new SqlCommand("INSERT INTO VoteRating VALUES(@Vote,@UserId)", con2))
{
cmd2.CommandType = CommandType.Text;
cmd2.Parameters.AddWithValue("@Vote", Convert.ToInt32(vote));
cmd2.Parameters.AddWithValue("@UserId", userId);
con2.Open();
cmd2.ExecuteNonQuery();
con2.Close();
}
}
}
}
SQL:
CREATE TABLE [dbo].[VoteRating](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Vote] [int] NULL,
[UserId] [int] NULL,
CONSTRAINT [PK_VoteRating] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO