In this article I will explain with an example, how to use the ASP.Net AJAX Control Toolkit Rating Extender Control in ASP.Net.
	
		 
	
		Database
	
		For this article I have created a custom database the script of which is attached in the sample zip. The database has the following table.
	![ASP.Net AJAX Rating Control Example]() 
	
		The Id column is an INTEGER column and also is set IDENTITY ON. While the Rating column is a SMALLINT column to store the rating values from range 1 – 5.
	
		 
	
		 
	
		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 is simple, it contains of an ASP.Net AJAX Control Toolkit Rating Extender Control and a Label control.
	
		
			<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
		
			</cc1:ToolkitScriptManager>
		
			<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 namespaces.
	
		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
	
		In the HTML Markup you will notice that I have set AutoPostBack property to true and also have set the OnChanged event for the Rating Extender control.
	
		Thus when 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 INTO UserRatings 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 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"]);
		
			    }
		
			}
		
			 
		
			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 Control Example]() 
	
		 
	
		 
	
		Demo
	
	
		 
	
		 
	
		Downloads