In this article I will explain with an example, how to use ASP.Net AJAX Control Toolkit Rating Extender Control with a simple example and will also describe
1. How to populate Average of the Total Ratings from Database and display it in the Rating Extender control.
2. How to save and update the User Ratings in Database.
 
 

Database

I have made use of the following table User Ratings with the schema as follow.
ASP.Net AJAX Rating Extender Control Database Example – How to save and display Average Rating value from database
 
 

Using the ASP.Net AJAX Control Toolkit Rating Extender Control

1. Drag an ASP.Net AJAX ToolScriptManager on the page.
2. Register the AJAX Control Toolkit Library after adding reference to your project.
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
 
 

HTML Markup

The HTML Markup consists of following controls:
ScriptManager – For enabling ASP.Net AJAX.
Rating- Allows users to select the number of stars that represents their rating.
Label – For showing the rating value.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:Rating ID="Rating1" AutoPostBack="true" OnChanged="OnRatingChanged" runat="server"
    StarCssClass="Star" WaitingStarCssClass="WaitingStar" EmptyStarCssClass="Star"
    FilledStarCssClass="FilledStar">
</cc1:Rating>
<br />
<asp:Label ID="lblRatingStatus" runat="server" Text=""></asp:Label>
 
 

Namespaces

You will need to import the following namespace.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using AjaxControlToolkit;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports AjaxControlToolkit
 
 

Inserting and saving the User Ratings to SQL Server Database Table

When the user clicks on any of the Stars the following event is fired where the User Selected Rating is available in the RatingEventArgs Value property, which is then finally inserted into database.
C#
protected void OnRatingChanged(object sender, RatingEventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("INSERT INTO UserRatings VALUES(@Rating)"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Rating", e.Value);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);
}
 
VB.Net
Protected Sub OnRatingChanged(sender As Object, e As RatingEventArgs)
    Dim constr As String ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand("INSERT INTOUserRatings VALUES(@Rating)")
            Using sda As New SqlDataAdapter()
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@Rating", e.Value)
                cmd.Connection = con
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
            End Using
        End Using
    End Using
    Response.Redirect(Request.Url.AbsoluteUri)
End Sub
 
 

Populating the Average of the Total User Ratings from SQL Server Database Table

Now the Average of the Total User Ratings is calculated using the AVG Aggregate function of SQL Server and is set to the CurrentRating property of the Rating Extender Control.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = this.GetData("SELECT ISNULL(AVG(Rating), 0)AverageRating, COUNT(Rating)RatingCount  FROMUserRatings");
         Rating1.CurrentRating = Convert.ToInt32(dt.Rows[0]["AverageRating"]);
        lblRatingStatus.Text = string.Format("{0} Users have rated. Average Rating {1}", dt.Rows[0]["RatingCount"], dt.Rows[0]["AverageRating"]);
    }
}
 
private DataTable GetData(string query)
{
    DataTable dt = new DataTable();
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                sda.Fill(dt);
            }
        }
        return dt;
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = Me.GetData("SELECT ISNULL(AVG(Rating), 0)AverageRating, COUNT(Rating) RatingCount FROM UserRatings")
         Rating1.CurrentRating = Convert.ToInt32(dt.Rows(0)("AverageRating"))
        lblRatingStatus.Text = String.Format("{0} Users have rated. Average Rating {1}", dt.Rows(0)("RatingCount"), dt.Rows(0)("AverageRating"))
    End If
End Sub
Private Function GetData(query As String) As DataTable
    Dim dt As New DataTable()
    Dim constr As String  ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand(query)
            Using sda As New SqlDataAdapter()
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                sda.SelectCommand = cmd
                sda.Fill(dt)
            End Using
        End Using
        Return dt
    End Using
End Function
 
 

Screenshot

ASP.Net AJAX Rating Extender Control Database Example – How to save and display Average Rating value from database
 
 

Demo

 
 

Downloads