In this article I will explain how to show average rating with decimal values in asp.net using JQuery.
In previous post I explained Ajax rating example with database in asp.net . If we use ajax rating control to display rating we have one disadvantage that is if we get average rating like 4.3 or 2.7 etc we don’t have chance to show rating based on that value because ajax rating supports only integer values to solve this problem I am writing this article to display average rating including decimal values using JQuery in asp.net.
Column Name | Data Type | Allow Nulls |
Id | int(set identity property=true) | No |
Rating | float | Yes |
UserName | varchar(50) | Yes |
<%@ 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=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"); con.Open(); SqlCommand cmd = new SqlCommand("insert into Rating_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; } } } |
<%@ WebHandler Language="VB" Class=" RatingHandler" %> Imports System.Data Imports System.Data.SqlClient Imports System.Web Public Class RatingHandler Implements IHttpHandler Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest Dim con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB") con.Open() Dim cmd As New SqlCommand("insert into Rating_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) Dim da As New SqlDataAdapter(cmd) Dim dt As New DataTable() da.Fill(dt) Dim COUNT As Single = Single.Parse(dt.Rows(0)("NumberOfUsers").ToString()) Dim RATING As Single = Single.Parse(dt.Rows(0)("Total").ToString()) context.Response.ContentType = "text/plain" Try Dim result As Single = RATING / COUNT context.Response.Write(result.ToString("0.0")) Catch ex As Exception context.Response.StatusCode = 202 context.Response.Write(ex.Message) context.Response.Flush() context.Response.[End]() End Try End Sub Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable Get Return False End Get End Property End Class |
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>jQuery Rater Plugin Demo</title> <link type="text/css" rel="stylesheet" href="style.css" /> <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="testRater" class="stat"> <label for="rating">Rating</label> <div class="statVal"> <span class="ui-rater"> <span class="ui-rater-starsOff" style="width:90px;"><span class="ui-rater-starsOn" runat="server" id="testSpan"></span></span> <span class="ui-rater-rating" id="avgrating" runat="server"></span> (<span class="ui-rater-rateCount" id="userscount" runat="server"></span>) </span> </div> </div> </form> </body> </html> |
$('#testRater').rater({ postHref: 'RatingHandler.ashx' }); |
using System; using System.Data; using System.Data.SqlClient; |
protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"); con.Open(); SqlCommand 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=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(); } |
Imports System.Data Imports System.Data.SqlClient Partial Class Default2 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB") con.Open() Dim cmd As New SqlCommand("select count(Id) as NumberOfUsers,sum(Rating) as Total from Rating_Table", con) Dim da As New SqlDataAdapter(cmd) Dim dt As New DataTable() da.Fill(dt) Dim count As Single = 0, rating As Single = 0, result As Single = 0 If Convert.ToInt32(dt.Rows(0)("NumberOfUsers").ToString()) <> 0 Then count = Single.Parse(dt.Rows(0)("NumberOfUsers").ToString()) rating = Single.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" End If testSpan.Style.Add("width", result & "px") userscount.InnerText = count.ToString() End Sub End Class | |
![]() | |
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|


Subscribe by RSS
Subscribe by Email
12 comments :
Fabulous work sir ,great fan of you..
Fabulous work sir ,great fan of you..
Sir You Are Best Best Best Programmer For Me
Thank you Sir
Your Friend Rahul Shimpi
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
my web.config file is
what is problem
@Jack...
That problem because of database connection check this link SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"); Here i given my database connection please change this one and give your database connection string to connect your database. If that is not solved then check this post http://www.aspdotnet-suresh.com/2011/11/network-related-or-instance-specific.html
Thanks for the great info you have done a great job.Keep sharing
Thank you a lot! But can you tell, is it possible to implement rating in asp.net datalist items?
very thnks....g8
its great, thanks suresh !
thanks for your valuable posts
Deep!
Good Example but what if i want to pass another Parameter like ID of Post for example
Thank you
Sir my NumberOfUsers=2 and Rating=31...your above code not working properly for me..