如何在我的Datalist中单独保存每个产品的评级 [英] How Can I Save Rating Of Each Product Individually In My Datalist

查看:66
本文介绍了如何在我的Datalist中单独保存每个产品的评级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
        }
    }





这是我用来评价我网站上产品的代码。但是每种产品都显示出相同的评级。如何在datalist中单独保存和显示每个产品的评级。



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.

推荐答案

您是否注意到在将数据插入 UserRating 表,您只发送评级而没有 ProductIds



这就是为什么它是插入所有行,因此当您选择数据时,它会显示所有产品的相同评级。
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.


这篇关于如何在我的Datalist中单独保存每个产品的评级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆