SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringRating"].ConnectionString); protected void Page_Load(object sender, EventArgs e) { Labelrate.Text = "0 Users have rated this Product"; Labelrt.Text = "Average rating for this Product is 0"; if (!IsPostBack) { BindRatings(); } } protected void Rating1_Changed(object sender, AjaxControlToolkit.RatingEventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO UserRating(Rating,ProductID) VALUES (@Rating)", con); cmd.Parameters.AddWithValue("@Rating", SqlDbType.Int).Value = Rating1.CurrentRating; cmd.ExecuteNonQuery(); con.Close(); BindRatings(); } public void BindRatings() { int Total = 0; con.Open(); SqlCommand cmd = new SqlCommand("SELECT Rating FROM UserRating", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { Total += Convert.ToInt32(dt.Rows[i][0].ToString()); } int Average = Total / (dt.Rows.Count); Rating1.CurrentRating = Average; Labelrate.Text = dt.Rows.Count + " " + "Users have rated this Product"; Labelrt.Text = "Average rating for this Product is" + " " + Convert.ToString(Average); } }
UserRating
ProductIds
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)