Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
        }
    }


This is the code i am using to rate products on my website. But every product is showing the same rating. How can i save and display rating for each product in datalist individually.
Posted
Updated 29-Jun-14 2:13am
v2
Comments
[no name] 29-Jun-14 6:09am    
You need to learn how to use a WHERE clause for your SQL.

1 solution

Do you notice that you while inserting data into UserRating Table, you are only sending the Rating and no ProductIds?

That is why it is getting inserted for all the Rows, so when you are selecting the data, it shows you same Rating for all the Products.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900