This is a stripped down version of a method i wrote to retrieve the score to the ASP.NET AJAX Control Toolkit Rating component from an ID. What it does is dynamically build the rating control and depending on the result of the database query i prefill the rating component.
GeSHi (csharp):
public int selectStarRating(int ID)
{
string sqlQuery = "select score from someTable where scoreID = @scoreID";
SqlCommand cmd =
new SqlCommand
(sqlQuery, conn
);
cmd.Parameters.Add("@scoreID", SqlDbType.Int);
cmd.Parameters["@scoreID"].Value = ID;
try
{
conn.Open();
a =
new SqlDataAdapter
(cmd
);
a.Fill(s);
conn.Close();
foreach (DataRow dr in s.Tables[0].Rows)
{
this.Score = (int)dr["Score"];
}
}
catch (Exception e)
{
System.Diagnostics.Debug.Write(e.Message);
}
return (int)Score;
}
Created by GeSHI 1.0.7.20
To fill your rating control you need to call the method like this:
GeSHi (csharp):
r.CurrentRating = v.selectStarRating(10);
Created by GeSHI 1.0.7.20
screenshotThe screenshot belows demonstrates the code above but in my own version it also display the question, the amount of people that rated and the average rating at the right that are given by the x amount of people.
