Click here to Skip to main content
15,886,518 members
Articles / Web Development / ASP.NET

Rating Demystified: Ajax Way, Amazon Way*

Rate me:
Please Sign up or sign in to vote.
4.73/5 (77 votes)
16 Aug 2008CPOL3 min read 178.4K   1.2K   136  
Ever wondered, how the amazon rating system works with multiple items in the same page, here is a simple article to describe the basic bare bones needed to create an asynchronous rating module using ASP.NET , SQL Server and ..... AJAX
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration; 

namespace Rating
{
	/// <summary>
	/// Summary description for image.
	/// </summary>
	public class ajaxRating : System.Web.UI.Page
	{
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			try
			{
				
					string p = "Select * from " + ConfigurationSettings.AppSettings["MyPhotos"] + " WHERE id = '" + Convert.ToInt32(Request.QueryString["id"]) + "'";
				
					clsDataAccess myDAR = new clsDataAccess();
					myDAR.openConnection(); 
			
					double myScore = Convert.ToDouble(myDAR.getValue(p,Convert.ToInt32(ConfigurationSettings.AppSettings["ScoreField"])));
					double myRatedBy = Convert.ToInt32(myDAR.getValue(p,Convert.ToInt32(ConfigurationSettings.AppSettings["RatedByField"])));
				
					double myCRating = Convert.ToDouble(Request.QueryString["Rating"]);
				    double myTotalRating =0.0;
					
					if ((Convert.ToInt32(Request.QueryString["Rating"])==1)||(Convert.ToInt32(Request.QueryString["Rating"])==2)||(Convert.ToInt32(Request.QueryString["Rating"])==3)||(Convert.ToInt32(Request.QueryString["Rating"])==4)||(Convert.ToInt32(Request.QueryString["Rating"])==5))
						myTotalRating = (myScore + myCRating)/(myRatedBy+1);
					else
						myTotalRating = (myScore)/(myRatedBy);
				
			
					string myTotalRatingString = "";

					if ((myTotalRating <1)&&(myTotalRating>0))
						myTotalRatingString = ".5";
					else if (myTotalRating ==1.0)
						myTotalRatingString = "1";
					else if ((myTotalRating >1)&&(myTotalRating<2))
						myTotalRatingString = "1.5";
					else if (myTotalRating ==2.0)
						myTotalRatingString = "2";
					else if ((myTotalRating >2)&&(myTotalRating<3))
						myTotalRatingString = "2.5";
					else if (myTotalRating ==3.0)
						myTotalRatingString = "3";
					else if ((myTotalRating >3)&&(myTotalRating<4))
						myTotalRatingString = "3.5";
					else if (myTotalRating ==4.0)
						myTotalRatingString = "4";
					else if ((myTotalRating >4)&&(myTotalRating<5))
						myTotalRatingString = "4.5";
					else if (myTotalRating ==5.0)
						myTotalRatingString = "5";
				
				int RatedBy =0;
				int TRating =0;
				if ((Convert.ToInt32(Request.QueryString["Rating"])==1)||(Convert.ToInt32(Request.QueryString["Rating"])==2)||(Convert.ToInt32(Request.QueryString["Rating"])==3)||(Convert.ToInt32(Request.QueryString["Rating"])==4)||(Convert.ToInt32(Request.QueryString["Rating"])==5))
				{
					RatedBy = Convert.ToInt32(myRatedBy)+1;
					TRating = Convert.ToInt32(myScore)+Convert.ToInt32(Request.QueryString["rating"]);
				}
				else
				{
					RatedBy = Convert.ToInt32(myRatedBy);
					TRating = Convert.ToInt32(myScore);
				}

					string q = "UPDATE " + ConfigurationSettings.AppSettings["MyPhotos"] + " SET Score = '" + TRating + " ' , RatedBy = '" + RatedBy + "' WHERE id = '" + Convert.ToInt32(Request.QueryString["id"]) + "'";
				
					clsDataAccess myDA = new clsDataAccess();
					myDA.openConnection(); 
					myDA.saveData(q);
					myDA.closeConnection(); 


			
				if ((Convert.ToInt32(Request.QueryString["Rating"])==1)||(Convert.ToInt32(Request.QueryString["Rating"])==2)||(Convert.ToInt32(Request.QueryString["Rating"])==3)||(Convert.ToInt32(Request.QueryString["Rating"])==4)||(Convert.ToInt32(Request.QueryString["Rating"])==5))
				{
					Response.Cookies["RatedAjax" + Convert.ToInt32(Request.QueryString["id"])].Value = Request.QueryString["Rating"].ToString();
				}

					Response.Write(myTotalRatingString);
				}
			

			catch (Exception SQLexc)
			{
				Response.Write("Read Failed : " + SQLexc.ToString());
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Teamcal AI
United States United States

Comments and Discussions