Click here to Skip to main content
16,020,080 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends i am trying to make a examination application in which i want to show random questions in labels on form.so that the examinees should not get every question in sequence.
how should i achieve it?pls tell me.
currently i am able to select only the first record of the data reader in label.here's the code
VB
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class Form1
    'Create ADO.NET objects.
    Public myConn As SqlConnection
    Public myCmd As SqlCommand
    Public myReader As SqlDataReader
    Public myDs As New DataSet
    Public myAdp As SqlDataAdapter
    Public results As String
    Public n As Integer
    

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        'Create a Connection object.
        myConn = New SqlConnection("Data Source=testvm;Initial Catalog=ULSLLIVE;Integrated Security=True;")
        'Create a Command object.
        myCmd = myConn.CreateCommand
        myCmd.CommandText = "SELECT * FROM questions"
        'Open the connection.
        myConn.Open()
        myReader = myCmd.ExecuteReader()
        myReader.Read()
        Me.Label1.Text = myReader("q")
        Me.RadioButton1.Text = myReader("a1")
        Me.RadioButton2.Text = myReader("a2")
        Me.RadioButton3.Text = myReader("a3")
        Me.RadioButton4.Text = myReader("a4")

    End Sub
End Class
Posted
Updated 2-Jun-13 19:25pm
v2

1 solution

If you change your sql to be
VB
myCmd.CommandText = "SELECT TOP 1 * FROM questions ORDER BY NEWID()
you will get a single random question.
If you want to get more than 1 then change the TOP 1. In that instance you will also need to continue reading the rest of the returned records using
VB
while(myReader.Read())
 
Share this answer
 
Comments
sachin bhise 3-Jun-13 2:55am    
thanks CHill60

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