Hello Maters...I wanna to work with rating control in AjaxToolkit Liabrery. I searched on google and got some code.
Code:
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 it working fine...but as i rate anything..It creates a new row in database and inserts rating values..
Please make some changes in the code that it updates the frist row instead of creating new row each time...Ihanks in advance