Pradeep kumar

Pradeep kumar

  • NA
  • 1
  • 689

Working with rating control in ajax toolkit

Sep 15 2014 3:21 AM
I created a rating web application.Here is the code...
 
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class Demo_Default : System.Web.UI.Page
{ SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CloudShopConnectionString1"].ConnectionString);
    
protected void RatingControlChanged(object sender, AjaxControlToolkit.RatingEventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into RatingDetails(Rate)values(@Rating)",con);
cmd.Parameters.AddWithValue("@Rating",Rating1.CurrentRating);
cmd.ExecuteNonQuery();
con.Close();
BindRatingControl();
}
protected void BindRatingControl()
{
    int total = 0;

    DataTable dt = new DataTable();
    con.Open();
    SqlCommand cmd = new SqlCommand("Select Rate from RatingDetails", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    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;

        Label1.Text = Convert.ToInt32(total).ToString();
       
        lbltxt.Text = dt.Rows.Count + "user(s) have rated this article";
    }
}
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            {
                BindRatingControl();
            }
    }
}
As this code creates new rows while user clicks on rating control...Plz make some changes that instead of creating rows each time it can update data in single row...Whereas RatingDetails is the name of sql table.Thanks