Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello I have a Problem to generate random questions according to Number of Questions actually i want randome question which first take the no of questions just like (5,10,15,20,25...) and on this basis show random questions ...
In my Form i am using Four ComboBoxes , 1 DataGridview and 1 Button Control ... ComBoBox1 is for Modules ,ComboBox2 is for Topics, ComboBox3 is for Levels and ComboBox4 is for No of Questions...
and when i select the module then shows topic of selected module to ComboBox2 and then i select the level then shows random question according to level this is working but i want that this work but with no of question means when we click on numbers then shows randome question according to numbers,levels,Module and topic.
My Code Is:

VB
Public Class Form1
    Dim cn As New SqlConnection("Data Source=NIDA-PC\SQLEXPRESS;Initial Catalog=Finaldb;Integrated Security=True")
    Dim da As New SqlDataAdapter
    Dim ds As New DataSet
    Dim qry As String
    Dim dt As New DataTable
    Dim dt1 As New DataTable
    Dim qry1 As String
    Dim qry3 As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cn.Close()
        cn.Open()
        qry = "Select Module_ID,Module_Name from Module_tbl"
        da = New SqlDataAdapter(qry, cn)
        ds = New DataSet()
        da.Fill(ds, "tab")
        ComboBox1.DataSource = ds.Tables("tab")
        ComboBox1.DisplayMember = "Module_Name"
        ComboBox1.ValueMember = "Module_ID"
        ComboBox1.Text = ""
        cn.Close()

        cn.Close()
        cn.Open()
        qry1 = "Select Topic_ID,Topic_Name from topic_tbl"
        da = New SqlDataAdapter(qry1, cn)
        ds = New DataSet()
        da.Fill(ds, "tab")
        ComboBox2.DataSource = ds.Tables("tab")
        ComboBox2.DisplayMember = "Topic_Name"
        ComboBox2.ValueMember = "Topic_ID"
        ComboBox2.Text = ""
        cn.Close()

        ComboBox3.Items.Add("1")
        ComboBox3.Items.Add("2")
        ComboBox3.Items.Add("3")

        ComboBox4.Items.Add("5")
        ComboBox4.Items.Add("15")
        ComboBox4.Items.Add("20")
        ComboBox4.Items.Add("25")
        ComboBox4.Items.Add("30")
        ComboBox4.Items.Add("35")
        ComboBox4.Items.Add("40")
        ComboBox4.Items.Add("45")
        ComboBox4.Items.Add("50")
        ComboBox4.Items.Add("55")
        ComboBox4.Items.Add("60")
        ComboBox4.Items.Add("65")
        ComboBox4.Items.Add("70")
        ComboBox4.Items.Add("75")
        ComboBox4.Items.Add("80")
        ComboBox4.Items.Add("85")
        ComboBox4.Items.Add("90")
        ComboBox4.Items.Add("95")
        ComboBox4.Items.Add("100")

        Me.WindowState = FormWindowState.Maximized

    End Sub
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        qry3 = "SELECT Question,Image_Data,Level_ID FROM Question_tbl where Level_ID= '" & ComboBox3.SelectedItem & "'And Topic_ID= '" & ComboBox2.SelectedValue.ToString & "' ORDER BY NEWID()"
        'qry3 = "SELECT Level_ID * From Question_tbl Order By NewID() "
        da = New SqlDataAdapter(qry3, cn)
        dt1.Clear()
        da.Fill(dt1)
        ComboBox2.Focus()
        ComboBox3.Focus()
        DataGridView1.DataSource = dt1
        cn.Close()
    End Sub
    Dim qry6 As String
    Dim Module_ID As Integer
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        cn.Close()
        cn.Open()
        Try
            Module_ID = Convert.ToInt32(ComboBox1.SelectedValue.ToString)
        Catch ex As Exception
            'MsgBox("Error Number")
            Return
        End Try
        'Exception handling
        'MsgBox(ComboBox1alue.ToString)
        cn.Close()

        cn.Close()
        cn.Open()
        qry6 = "Select Topic_ID,Topic_Name from Topic_tbl where Module_ID = '" & Module_ID & "'"
        da = New SqlDataAdapter(qry6, cn)
        dt.Clear()
        da.Fill(dt)
        ComboBox1.Focus()
        ComboBox2.DataSource = dt
        cn.Close()
    End Sub
End Class
Posted
Updated 30-Jun-14 19:17pm
v2

1 solution

This is my suggested approach:
First of all, each question in the database should have an unique key for identification purpose, let call it question_id,
Assuming there are 100 questions in your database with question_id values ranging from 1 to 100.
Now, say user wants to select 10 questions, then
1. in the code, generate 10 distinct random integers in the range of 1 to 100.
2. fire an sql to select those 10 questions from your database that have question_ids equal to the 10 random integers.
3. Do something with the 10 questions.
 
Share this answer
 
v3
Comments
Member 10853731 1-Jul-14 3:18am    
oh also using Question_ID but its not working plz check this query this is working but it reads the question_ID...

qry3 = "SELECT Question,Image_Data,Level_ID FROM Question_tbl where Question_ID='" & ComboBox4.SelectedItem & "'And Level_ID= '" & ComboBox3.SelectedItem & "'And Topic_ID= '" & ComboBox2.SelectedValue.ToString & "' ORDER BY NEWID()"

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