Answered by:
Randomize questions in VB.NET

Question
-
User-1578974752 posted
How can I randomize the questions in a test. There are 30 question in the Test1
When the user Max login for attempting the Test,Test1 must show randomized question(For eg-> Code must pick any 10 randomly from 30 questions)
Wednesday, August 29, 2018 3:08 AM
Answers
-
User-369506445 posted
hi
you can use Random class below like
Private r As New Random() Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim count As Integer = 5 Dim questions() As String = {"Question 1: ...", "Question 2: ...", "Question 3: ...", "Question 4: ...", "Question 5: ...", "Question 6: ...", "Question 7: ...", "Question 8: ...", "Question 9: ...", "Question 10: ...", "Question 11: ...", "Question 12: ...", "Question 13: ...", "Question 14: ...", "Question 15: ...", "Question 16: ...", "Question 17: ...", "Question 18: ...", "Question 19: ...", "Question 20: ..."} Dim indices() As Integer = Enumerable.Range(0, questions.Length - 1).OrderBy(Function(i) r.Next()).Take(count ).ToArray() For i = 0 To count -1 Response.Write (questions(indices(i)) + "<br />") Next End Sub
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 29, 2018 4:40 AM -
User-369506445 posted
first select your data from <g class="gr_ gr_15 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="15" data-gr-id="15"><g class="gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="8" data-gr-id="8">databse</g> ,</g> then store then into <g class="gr_ gr_12 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="12" data-gr-id="12">a array</g> and <g class="gr_ gr_13 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="13" data-gr-id="13">finally</g> you can use it like the last sample
please follow step by step
<g class="gr_ gr_14 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="14" data-gr-id="14">first</g> create a class called MYItems
Public Class MYItems Private _question As String Private _option1 As String Private _option2 As String Private _option3 As String Private _option4 As String Private _option5 As String Public Property Option1() As String Get Return _option1 End Get Set(ByVal value As String) _option1 = value End Set End Property Public Property Option2() As String Get Return _option2 End Get Set(ByVal value As String) _option2 = value End Set End Property Public Property Option3() As String Get Return _option3 End Get Set(ByVal value As String) _option3 = value End Set End Property Public Property Option4() As String Get Return _option4 End Get Set(ByVal value As String) _option4 = value End Set End Property Public Property Option5() As String Get Return _option5 End Get Set(ByVal value As String) _option5 = value End Set End Property Public Property Question() As String Get Return _question End Get Set(ByVal value As String) _question = value End Set End Property End Class
Then fetch from <g class="gr_ gr_9 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="9" data-gr-id="9">database</g>
Private Function fetchFromDB() As List(Of MYItems) Dim list As New List(Of MYItems ) Dim conn As New SqlConnection If conn.State = ConnectionState.Closed Then conn.ConnectionString = ("Data Source=.;Initial Catalog=Your Database Name;trusted_connection=true") End If Try conn.Open() Dim sqlquery As String = "SELECT [Question], [Option1], [Option2], [Option3], [Option4], [Option5] FROM [QuestionDetail] " Dim data As SqlDataReader Dim adapter As New SqlDataAdapter Dim parameter As New SqlParameter Dim command As SqlCommand = New SqlCommand(sqlquery, conn) command.Connection = conn adapter.SelectCommand = command data = command.ExecuteReader() While data.Read If data.HasRows = True Then Dim obj As MYItems =New MYItems obj.Question = data (0) obj.Option1 = data (1) obj.Option2 = data (2) obj.Option2 = data (3) obj.Option3 = data (4) obj.Option4 = data (5) obj.Option5 = data (6) list.Add(obj) End If End While Return list Catch ex As Exception Return list End Try End Function
finally bind to your control
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim count As Integer = 5 Dim questions As List(Of MYItems ) = fetchFromDB () Dim indices() As Integer = Enumerable.Range(0, questions.Count - 1).OrderBy(Function(i) r.Next()).Take(count ).ToArray() Dim model = New List(Of MYItems) For i = 0 To count -1 model.Add(questions(indices(i))) Next ListView1.DataSource = model ListView1.DataBind() End Sub
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 29, 2018 10:04 AM -
User-2015242085 posted
Hi
If questions are stored in SQL you can like this:
SELECT TOP 10 QuestionId FROM Questions ORDER BY NEWID()
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 31, 2018 1:00 AM
All replies
-
User-369506445 posted
hi
you can use Random class below like
Private r As New Random() Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim count As Integer = 5 Dim questions() As String = {"Question 1: ...", "Question 2: ...", "Question 3: ...", "Question 4: ...", "Question 5: ...", "Question 6: ...", "Question 7: ...", "Question 8: ...", "Question 9: ...", "Question 10: ...", "Question 11: ...", "Question 12: ...", "Question 13: ...", "Question 14: ...", "Question 15: ...", "Question 16: ...", "Question 17: ...", "Question 18: ...", "Question 19: ...", "Question 20: ..."} Dim indices() As Integer = Enumerable.Range(0, questions.Length - 1).OrderBy(Function(i) r.Next()).Take(count ).ToArray() For i = 0 To count -1 Response.Write (questions(indices(i)) + "<br />") Next End Sub
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 29, 2018 4:40 AM -
User-1578974752 posted
Hi Vahid bakkhi
How can I implement this inside a Listview control .Where questions are retrieved from the database.Thanks
<asp:Datapager ID="dtpgr" runat="server" pagedcontrolid="ListView1" Pagesize="5">
<Fields>
<asp:NumericPagerField ButtonCount="5"
NumericButtonCssClass="numeric_button"
CurrentPageLabelCssClass="current_page"
NextPreviousButtonCssClass="next_button" />
</Fields>
</asp:DataPager>
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource2">
<itemtemplate>
<div>
<td align="left"><%#DataBinder.Eval(Container.DataItem, "Question") %></td>
</div>
<table>
<tr style="background-color:White;">
<td align="right"><img src="images/Dot.gif"/></td>
<td align="center"><asp:RadioButton ID="rad1" GroupName="Group1" runat="server" /></td>
<td align="left"><%#DataBinder.Eval(Container.DataItem, "Option1") %></td>
<td align="left"><asp:RadioButton ID="rad2" GroupName="Group1" runat="server" OnSelectedIndexChanged="btnSave_Click" /></td>
<td align="left"><%#DataBinder.Eval(Container.DataItem, "Option2") %></td>
<td align="center"><asp:RadioButton ID="rad3" GroupName="Group1" runat="server" /></td>
<td align="left"><%#DataBinder.Eval(Container.DataItem, "Option3") %></td>
<td align="center"><asp:RadioButton ID="rad4" GroupName="Group1" runat="server" /></td>
<td align="left"><%#DataBinder.Eval(Container.DataItem, "Option4") %></td>
<td>
</td>
</table>
</itemtemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:QSCTestConnectionString %>" SelectCommand="SELECT [Question], [Option1], [Option2], [Option3], [Option4], [Option5] FROM [QuestionDetail]"></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Button" />
Wednesday, August 29, 2018 9:42 AM -
User-369506445 posted
first select your data from <g class="gr_ gr_15 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="15" data-gr-id="15"><g class="gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="8" data-gr-id="8">databse</g> ,</g> then store then into <g class="gr_ gr_12 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="12" data-gr-id="12">a array</g> and <g class="gr_ gr_13 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="13" data-gr-id="13">finally</g> you can use it like the last sample
please follow step by step
<g class="gr_ gr_14 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="14" data-gr-id="14">first</g> create a class called MYItems
Public Class MYItems Private _question As String Private _option1 As String Private _option2 As String Private _option3 As String Private _option4 As String Private _option5 As String Public Property Option1() As String Get Return _option1 End Get Set(ByVal value As String) _option1 = value End Set End Property Public Property Option2() As String Get Return _option2 End Get Set(ByVal value As String) _option2 = value End Set End Property Public Property Option3() As String Get Return _option3 End Get Set(ByVal value As String) _option3 = value End Set End Property Public Property Option4() As String Get Return _option4 End Get Set(ByVal value As String) _option4 = value End Set End Property Public Property Option5() As String Get Return _option5 End Get Set(ByVal value As String) _option5 = value End Set End Property Public Property Question() As String Get Return _question End Get Set(ByVal value As String) _question = value End Set End Property End Class
Then fetch from <g class="gr_ gr_9 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="9" data-gr-id="9">database</g>
Private Function fetchFromDB() As List(Of MYItems) Dim list As New List(Of MYItems ) Dim conn As New SqlConnection If conn.State = ConnectionState.Closed Then conn.ConnectionString = ("Data Source=.;Initial Catalog=Your Database Name;trusted_connection=true") End If Try conn.Open() Dim sqlquery As String = "SELECT [Question], [Option1], [Option2], [Option3], [Option4], [Option5] FROM [QuestionDetail] " Dim data As SqlDataReader Dim adapter As New SqlDataAdapter Dim parameter As New SqlParameter Dim command As SqlCommand = New SqlCommand(sqlquery, conn) command.Connection = conn adapter.SelectCommand = command data = command.ExecuteReader() While data.Read If data.HasRows = True Then Dim obj As MYItems =New MYItems obj.Question = data (0) obj.Option1 = data (1) obj.Option2 = data (2) obj.Option2 = data (3) obj.Option3 = data (4) obj.Option4 = data (5) obj.Option5 = data (6) list.Add(obj) End If End While Return list Catch ex As Exception Return list End Try End Function
finally bind to your control
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim count As Integer = 5 Dim questions As List(Of MYItems ) = fetchFromDB () Dim indices() As Integer = Enumerable.Range(0, questions.Count - 1).OrderBy(Function(i) r.Next()).Take(count ).ToArray() Dim model = New List(Of MYItems) For i = 0 To count -1 model.Add(questions(indices(i))) Next ListView1.DataSource = model ListView1.DataBind() End Sub
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 29, 2018 10:04 AM -
User-1578974752 posted
Dim indices() As Integer = Enumerable.Range(0, questions.Count - 1).OrderBy(Function(i) r.Next()).Take(count).ToArray()
here r is not declared.It may be in accessible due to its protection level message is showing.Tried to declare as integer..still error
Thursday, August 30, 2018 2:07 AM -
User-369506445 posted
Private r As New Random()
Thursday, August 30, 2018 5:14 AM -
User-2015242085 posted
Hi
If questions are stored in SQL you can like this:
SELECT TOP 10 QuestionId FROM Questions ORDER BY NEWID()
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 31, 2018 1:00 AM -
User-1578974752 posted
Working fine . But I have 3 pages in the listview.Each page will contain 5 questions.so when I do as below, some times the questions in page 1 in repeating in Page2.How can I solve that.Thanks
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>" SelectCommand="SELECT TOP 15 [Question],[CorrectAnswer],[Option1], [Option2], [Option3], [Option4], [Option5], [QuestDetailID], [QuestionMasterID] FROM [QuestionDetail] WHERE ([QuestionMasterID] = @QuestionMasterID) ORDER BY NEWID()">
Friday, August 31, 2018 7:21 AM