Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dear sir/ma'am

i have created a quiz site .sir all things works well but one problem i found that i want to fetch questions randomly in my question list how can i do it sir
help me

how can i achiw=eve the goal to fetch questions randomly for each student let i have 5 question then all questions are shown to every student but randomly


sir how this query can be written correctly
SELECT     *
FROM         Quizquestion
WHERE     (Quiz_Id = '1417')
ORDER BY ques_id RAND()

random quesid fetch

my get question code is
str = "select * from quizdetails where quiz_id = '" & quizid & "'"
        cmdly.rst1 = New DataTable
        cmdly.rst1 = cmdly.executenonquery(str)

       
        str = "select  TOP 1 ques_id, question, correctoption, Quiz_Id from quizquestion where quiz_id = '" & quizid & "' ORDER BY NEWID()"
        cmdly.rst2 = New DataTable
        cmdly.rst2 = cmdly.executenonquery(str)
        If cmdly.rst2.Rows.Count > 0 Then

            lbquescnt.Text = quesid
            lbques.Text = "Q. " & cmdly.rst2.Rows(0).Item("question").ToString

            cmdly.rst3 = New DataTable
            str = "select * from quizoption where quiz_id = '" & quizid & "' and ques_id  = '" & cmdly.rst2.Rows(0).Item("ques_id") & "'"
            cmdly.rst3 = cmdly.executenonquery(str)

            If cmdly.rst3.Rows.Count > 0 Then
                i = cmdly.rst3.Rows.Count
                c = 0
                quizlst.Items.Clear()

                While c < i
                    Dim qlist As New System.Web.UI.WebControls.ListItem
                    qlist.Text = cmdly.rst3.Rows(c).Item("optionVALUE").ToString
                    qlist.Value = cmdly.rst3.Rows(c).Item("option_id").ToString
                    quizlst.Items.Add(qlist)
                    c = c + 1
                End While
            End If
        End If
Posted
Updated 27-Jun-13 1:23am
v6
Comments
Ankur\m/ 27-Jun-13 5:11am    
How do you fetch the questions - all at a time or one by one from the db?
ankur789 27-Jun-13 5:16am    
one by one
Maciej Los 27-Jun-13 5:22am    
What have you done so far?

You can try "select * from tblTableName order by NEWID()"
 
Share this answer
 
Use Random clase to generate the random question numbers and then use this number to fetch question. The below sample generated 10 random number between 1-20. In your case use the count of questions available in qustion bank.

C#
Random r = new Random();
for (int i = 0; i < 10; i++)
{
      int x= r.Next(20);
      MessageBox.Show( x.ToString());
}
 
Share this answer
 
See the final example in this page: "SQL - RAND Function"[^].
 
Share this answer
 
if you want only one at a time

SELECT top 1 (columnname) FROM Quizquestion ORDER BY Newid()
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900