0

How to randomly display 10 question from database? How can i check the answer is correct with database or not?

        db.command(true, "SELECT * FROM question WHERE Age_group='" +category + "'");
        foreach (DataRow item in db.result.Rows)
        {
            question_list.Add (Convert.ToInt32(item["id"]));
        }

        for (int i = 0; i < max_question; i++)
        {
            int index = ran.Next(question_list.Count);
            question_choose.Add(question_list[index]);
            question_list.Remove(question_list[index]);
        }

1 Answers1

1
select top 10 * from table order by newid()

also, see "order by newid()" - how does it work?

Community
  • 1
  • 1
Unix von Bash
  • 735
  • 5
  • 20
  • ya, it's work. How can i random 10 question belong to the age category that i choose? – user2948178 Nov 03 '13 at 14:08
  • you can try select top 10 * from table where age>10 order by newid() – Unix von Bash Nov 03 '13 at 14:25
  • that's not work, i got try SELECT top 15 * FROM question where age_group='" +category+ "order by newid()" but still not work too – user2948178 Nov 03 '13 at 14:49
  • in your example, you have one extra single quote before category -> age_group = ' " +category.... – Unix von Bash Nov 03 '13 at 15:11
  • ya, In the form, i will put 3 type of category which is 6-10, 11-15, 16-20 to let the user choose, at the last how can i know the age category choose by user and random the question to them? – user2948178 Nov 03 '13 at 15:45
  • Not sure if I understand your question. If you ask how to get questions from an age interval, it'll be: Where age>6 AND age<10. If you mean, how to know what user has been chose, you can make a radio button menu which returns 1 if he selected 6-10, returns 2 if selected 11-15 and so on. Than, comparing this number, you select what condition to add in the query: if(radioBtn==1) { querry +=" Where age>6 AND age<10";} else querry+= "Where age>10 AND age<16"; – Unix von Bash Nov 03 '13 at 15:54