0

Good day everyone,

so here, i have a problem with my repeater control's sql query, what i would really want to happen, is i have a randomized number from 1 to 60, that will tell the database which test question to select, (if it's only one, it's not a problem, haha.) but then, i am required to display 20 questions from my database to the repeater control. my database also holds the 60 questions. so yeah, i tried some solutions in the net, but then my code always returns only one record, and it's quite a pain. hope somebody could help me, i know to some it's quite basic, but i'm only a budding programmer, so yeah, thanks in advance.

btw, this is the chunk of code,

   protected void Page_Load(object sender, EventArgs e)
    {
        int i = 1;
        Random r = new Random();
            while (i <= 20)
            {
                int iss;
                string constr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Mark\Desktop\thesis\WebSite8\App_Data\thesisDB.mdf;Integrated Security=True;User Instance=True";
                SqlConnection con = new SqlConnection(constr);
                string com = "SELECT * from CHAP1_quiz WHERE questionnumber = @num";
                SqlDataAdapter comms = new SqlDataAdapter();
                SqlCommand cmd = new SqlCommand(com, con);
                iss = r.Next(1, 60);
                comms.SelectCommand = cmd;
                cmd.Parameters.Add("@num");
                DataSet ds = new DataSet();
                con.Open();
                comms.Fill(ds);
                Repeater1.DataSource = ds;
                Repeater1.DataBind();
                i++;;
                con.Close();
            }

    }`
2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by __avd
0

Use,

SELECT TOP 20 * FROM from CHAP1_quiz ORDER BY newid()

This SQL statement will fetch 20 rows (random order by NEWID()) from the database.

Code block:

using(SqlConnection connection=new SqlConnection(constr))
{
 string sql="SELECT TOP 20 * FROM from CHAP1_quiz ORDER BY newid()";
 using(SqlDataAdpater adp=new SqlDataAdapter(sql,connection))
  {
   DataTable dataTable=new DataTable();
   adp.Fill(dataTable);
   Repeater1.DataSource = dataTable;
   Repeater1.DataBind();
   }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.