ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Bootstrap Star Rating plugin with Average Rating Value from database using jQuery Ajax in ASP.Net<p>Hi&nbsp;IamAzhar,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <pre class="brush: sql">CREATE TABLE UserRatings ( Id INT PRIMARY KEY IDENTITY, Rating NUMERIC(5, 2) ) </pre> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;link href=&#34;http://netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css&#34; rel=&#34;stylesheet&#34;&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-star-rating/4.0.2/css/star-rating.min.css&#34; /&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/bootstrap-star-rating/4.0.2/js/star-rating.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { GetRatings(); $(&#39;.filled-stars&#39;).click(function () { var value = parseInt($(this)[0].style.width.replace(&#39;%&#39;)) / 20; $.ajax({ type: &#34;POST&#34;, url: &#34;Default.aspx/Rate&#34;, data: &#34;{rating: &#34; + value + &#34;}&#34;, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: function (response) { alert(&#34;Your rating has been saved.&#34;); GetRatings(); }, failure: function (response) { alert(&#39;There was an error.&#39;); } }); }); }); function GetRatings() { $.ajax({ type: &#34;POST&#34;, url: &#34;Default.aspx/GetRating&#34;, data: &#34;{}&#34;, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: function (response) { var result = eval(response.d)[0]; if (result.Average &gt; 0) { $(&#39;.filled-stars&#39;)[0].style.width = result.Average * 20 + &#34;%&#34;; $(&#34;#rating&#34;).html(&#34;Average Rating: &#34; + result.Average + &#34; Total Rating: &#34; + result.Total); } else { $(&#39;.filled-stars&#39;)[0].style.width = &#34;0%&#34;; $(&#34;#rating&#34;).html(&#34;Average Rating: 0 Total Rating: 0&#34;); } }, failure: function (response) { alert(&#39;There was an error.&#39;); } }); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;div&gt; &lt;br /&gt; &lt;input id=&#34;rating_star&#34; class=&#34;rating rating-loading&#34; value=&#34;0&#34; data-min=&#34;0&#34; data-max=&#34;5&#34; data-step=&#34;0.5&#34; data-size=&#34;xs&#34;&gt; &lt;hr /&gt; &lt;span id=&#34;rating&#34;&gt;&lt;/span&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespace</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Web.Services;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Configuration Imports System.Data Imports System.Data.SqlClient Imports System.Web.Services</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">[WebMethod] public static string GetRating() { string sql = &#34;SELECT ROUND(ISNULL(CAST(SUM(Rating) AS NUMERIC(5, 2)) / COUNT(Rating), 0), 1) Average&#34;; sql += &#34;, COUNT(Rating) Total FROM UserRatings&#34;; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(sql)) { cmd.CommandType = CommandType.Text; cmd.Connection = con; con.Open(); string json = string.Empty; using (SqlDataReader sdr = cmd.ExecuteReader()) { sdr.Read(); json += &#34;[ {&#34;; json += string.Format(&#34;Average: {0}, Total: {1}&#34;, sdr[&#34;Average&#34;], sdr[&#34;Total&#34;]); json += &#34;} ]&#34;; sdr.Close(); } con.Close(); return json; } } } [WebMethod] public static void Rate(decimal rating) { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;INSERT INTO UserRatings VALUES(@Rating)&#34;)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue(&#34;@Rating&#34;, rating); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">&lt;WebMethod()&gt; Public Shared Function GetRating() As String Dim sql As String = &#34;SELECT ROUND(ISNULL(CAST(SUM(Rating) AS NUMERIC(5, 2)) / COUNT(Rating), 0), 1) Average&#34; sql += &#34;, COUNT(Rating) Total FROM UserRatings&#34; Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(sql) cmd.CommandType = CommandType.Text cmd.Connection = con con.Open() Dim json As String = String.Empty Using sdr As SqlDataReader = cmd.ExecuteReader() sdr.Read() json += &#34;[ {&#34; json += String.Format(&#34;Average: {0}, Total: {1}&#34;, sdr(&#34;Average&#34;), sdr(&#34;Total&#34;)) json += &#34;} ]&#34; sdr.Close() End Using con.Close() Return json End Using End Using End Function &lt;WebMethod()&gt; Public Shared Sub Rate(ByVal rating As Decimal) Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;INSERT INTO UserRatings VALUES(@Rating)&#34;) Using sda As SqlDataAdapter = New SqlDataAdapter() cmd.CommandType = CommandType.Text cmd.Parameters.AddWithValue(&#34;@Rating&#34;, rating) cmd.Connection = con con.Open() cmd.ExecuteNonQuery() con.Close() End Using End Using End Using End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/KIAT0i9.gif" alt="" width="338" height="200" /></span></strong></p>https://www.aspforums.net:443/Threads/807587/Bootstrap-Star-Rating-plugin-with-Average-Rating-Value-from-database-using-jQuery-Ajax-in-ASPNet/https://www.aspforums.net:443/Threads/807587/Bootstrap-Star-Rating-plugin-with-Average-Rating-Value-from-database-using-jQuery-Ajax-in-ASPNet/Mon, 22 Jul 2019 05:34:10 GMT