Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Prefill the AJAX Control Toolkit Rating component with data from database  (Read 809 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: August 02, 2009, 08:06:00 AM »

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.

      
Code
GeSHi (csharp):
  1.  public int selectStarRating(int ID)
  2.        {
  3.            string sqlQuery = "select score from someTable where scoreID = @scoreID";
  4.  
  5.            SqlCommand cmd = new SqlCommand(sqlQuery, conn);            
  6.            cmd.Parameters.Add("@scoreID", SqlDbType.Int);
  7.            cmd.Parameters["@scoreID"].Value = ID;
  8.  
  9.            try
  10.            {
  11.                conn.Open();
  12.                a = new SqlDataAdapter(cmd);
  13.                s = new DataSet();
  14.                a.Fill(s);
  15.                conn.Close();
  16.  
  17.                foreach (DataRow dr in s.Tables[0].Rows)
  18.                {
  19.                    this.Score = (int)dr["Score"];
  20.                }
  21.            }
  22.            catch (Exception e)
  23.            {
  24.                System.Diagnostics.Debug.Write(e.Message);
  25.            }
  26.            return (int)Score;
  27.        }
Created by GeSHI 1.0.7.20

To fill your rating control you need to call the method like this:

Code
GeSHi (csharp):
  1. r.CurrentRating = v.selectStarRating(10);
Created by GeSHI 1.0.7.20

screenshot
The 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.



« Last Edit: August 02, 2009, 08:43:30 AM by HappyFace » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here