Disply average rating with decimal values from database in asp.net
Description:
In this example i will explain how to show average rating with decimal values in asp.net using jquery.
DataTable:Rate_table
Columnname Datatype AllowNulls
id int(set identity property=true) No
Rating float Yes
UserName varchar(50) Yes
After
completion table design create new website using visual studio and
Right click on your website >> select Add New Item >> Select
Generic Handler and give name as RateHandle.ashx
C# Code:
<%@ WebHandler Language="C#" Class="RatingHandler" %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
public class RatingHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
SqlConnection con = new SqlConnection("Data Source=dhaval;Integrated Security=true;Initial Catalog=MySampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("insert into Rate_Table(Rating,UserName) values(@rating,@name)", con);
cmd.Parameters.AddWithValue("@rating", context.Request.Form["rating"]);
cmd.Parameters.AddWithValue("@name", context.Request.LogonUserIdentity.Name);
cmd.ExecuteNonQuery();
cmd = new SqlCommand("select count(Id) as NumberOfUsers,sum(Rating) as Total from Rating_Table", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
float COUNT = float.Parse(dt.Rows[0]["NumberOfUsers"].ToString());
float RATING = float.Parse(dt.Rows[0]["Total"].ToString());
context.Response.ContentType = "text/plain";
try
{
float result = RATING / COUNT;
context.Response.Write(result.ToString("0.0"));
}
catch (Exception ex)
{
context.Response.StatusCode = 202;
context.Response.Write(ex.Message);
context.Response.Flush();
context.Response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}After completion of write code in HttpHandler add following code in your aspx page
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Rating Demo</title>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="jquery.rater.js"></script>
<script type="text/javascript">
$(function() {
$('#testRater').rater({ postHref: 'RatingHandler.ashx' });
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="testRate" class="stat">
<label for="rating">Rating</label>
<div class="Val">
<span class="rater">
<span class="rater-starsOff" style="width:90px;"><span class="rater-starsOn" runat="server" id="testSpan"></span></span>
<span class="rater-rating" id="rating" runat="server"></span> (<span class="rater-rateCount" id="count" runat="server"></span>)
</span>
</div>
</div>
</form>
</body>
</html>After completion of aspx page design add the following namespaces in code behind
C# Code
using System;
using System.Data;
using System.Data.SqlClient;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=Dhaval;Integrated Security=true;Initial Catalog=MySampleDB");
con.Open();
SqlCommand cmd = new SqlCommand("select count(Id) as NumberOfUsers,sum(Rating) as Total from Rate_Table", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
float count=0,rating=0,result=0;
if( Convert.ToInt32(dt.Rows[0]["NumberOfUsers"].ToString())!=0 )
{
count = float.Parse(dt.Rows[0]["NumberOfUsers"].ToString());
rating = float.Parse(dt.Rows[0]["Total"].ToString());
result = Convert.ToInt32(Math.Ceiling((rating / count) * 18));
avgrating.InnerText = Math.Round((rating / count), 1).ToString();
}
else
{
avgrating.InnerText = "0";
}
testSpan.Style.Add("width", result+"px");
userscount.InnerText = count.ToString();
}
No comments:
Post a Comment