randomly select quiz questions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: randomly select quiz questions

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    51

    randomly select quiz questions

    Halor..I am currently doing an online quiz with vb.net and sql server 2000. 10 questions will be randomly selected from 30 questions. I random select the questions using query. Now I would like to store 10 of the questions into an array but do not know how to do so. Please help.

    This is the code I had written:

    Sub Populate()

    Dim cnn As SqlConnection = _
    New SqlClient.SqlConnection _
    ("Data Source=(local);" & _
    "Integrated Security=SSPI;" & _
    "Initial Catalog=EELANE")



    Dim cmd As SqlClient.SqlCommand = _
    New SqlClient.SqlCommand("SELECT TOP 5 * FROM QUIZ", cnn)
    Dim dap As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
    dap.SelectCommand = cmd
    cnn.Open()

    'Populate the dataset
    dap.Fill(das, "QUIZ")

    Dim cmd1 As SqlClient.SqlCommand = _
    New SqlClient.SqlCommand _
    ("SELECT quiz_id, answer_id, answer_choice, answer_remark " & _
    "FROM QUIZ,ANSWER_QUIZ WHERE QUIZ.quiz_id = ANSWER_QUIZ.quiz_id", _
    cnn)

    dap.SelectCommand = cmd1
    cnn.Open()

    'Populate the dataset
    dap.Fill(das, "QUIZ_ANSWER")

    'Close the connection because a dataset is a disconnected data source
    cnn.Close()

    End Sub
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    'Call routine for creating dataset
    Populate()

    'Bind quiz questions to each panel

    Dim Question(10)
    Dim Answer1()
    Dim Answer2()
    Dim Answer3()
    Dim Answer4()
    Last edited by galgal5814; 03-10-2006 at 04:05 AM.

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    You already have the questions in a dataset; why do you want to copy them to an array? You may simply bind the dataset to controls on the page.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Dec 2005
    Posts
    51
    erm...because I choose it at random..u mean eventhough i select random i can just bind de dataset to the control of the page without storing in the array? sorry I am very new at this

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Yes, even though you select random questions, you can bind the dataset to the page. The dataset and the page don't care that you're selecting random questions; they work the same as they would if you were selecting an entire table.

    In your code above, it looks like you're selecting the first five questions in the table: SELECT TOP 5 * FROM Quiz. If you want to select 10 questions at random, try this: SELECT TOP 10 * FROM Quiz ORDER BY NewID().
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    Dec 2005
    Posts
    51
    ooo thank you so much Phil..

  6. #6
    Join Date
    Dec 2005
    Posts
    51
    Sorry I had 1 more enquiry.
    I want to bind each question to different textbox. How can I select the 1st randomly chose questions to be bind with the 1st question to the 1st textbox..2nd question to the second textbox and so on?
    Last edited by galgal5814; 03-10-2006 at 12:39 PM.

Similar Threads

  1. How do I find duplicate records?
    By Nancy in forum Database
    Replies: 2
    Last Post: 01-11-2002, 10:38 PM
  2. SQL Tutorial (Answer Q's & post your reply)
    By bigbastard4 in forum Database
    Replies: 2
    Last Post: 05-16-2001, 07:24 PM
  3. Get 10 months forward with select statment
    By Nirit Touboul in forum Database
    Replies: 7
    Last Post: 02-25-2001, 12:34 PM
  4. Stored Procedure (Want looping in Cursor)
    By malikmehmood in forum Database
    Replies: 0
    Last Post: 11-20-2000, 06:12 AM
  5. Replies: 0
    Last Post: 03-18-2000, 10:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center