title: Random Questions using DetailsView (Help)
body: <p>Greetings everyone, i would just like to ask how do i retrieve data rows in my table in my database randomly... i was able to create an online quiz wherein it displays the question,choices in consecutive order but what i want is, every time when user will start  a quiz it will show questions in random order. I am using mssql 2005 as my database below is my code.. any advice or suggestions is highly sought.. thank you and have a great day..</p>
  
  <blockquote>
    <p>QuizPage.aspx</p>
  </blockquote>
  
  <pre><code>&lt;asp:DetailsView ID="questionDetails" runat="server" AutoGenerateRows="False" 
                  CellPadding="4" ForeColor="#333333" 
                  GridLines="None" Height="50px" Width="550px"&gt;
                  &lt;FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
                  &lt;CommandRowStyle BackColor="#E2DED6" Font-Bold="True" /&gt;
                  &lt;RowStyle BackColor="#F7F6F3" CssClass="generaltext" ForeColor="#333333" /&gt;
                  &lt;FieldHeaderStyle BackColor="#E9ECF1" CssClass="boldtext" Font-Bold="True" 
                      Width="80px" /&gt;
                  &lt;PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /&gt;
                  &lt;Fields&gt;
                      &lt;asp:TemplateField HeaderText="Question ID"&gt;           
                          &lt;ItemTemplate&gt;
                                         &lt;asp:Label ID="question_id" runat="server" Text='&lt;%# Bind("question_id") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;                                                                             
                          &lt;/asp:TemplateField&gt; 
                           &lt;asp:TemplateField HeaderText="Question:"&gt;           
                          &lt;ItemTemplate&gt;
                                         &lt;asp:Label ID="quiz_question" runat="server" Text='&lt;%# Bind("quiz_question") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;                                                                             
                          &lt;/asp:TemplateField&gt; 
                           &lt;asp:TemplateField HeaderText="Choice 1:"&gt;           
                          &lt;ItemTemplate&gt;
                                         &lt;asp:Label ID="choice1" runat="server" Text='&lt;%# Bind("choice1") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;                                                                             
                          &lt;/asp:TemplateField&gt; 
                           &lt;asp:TemplateField HeaderText="Choice 2:"&gt;           
                          &lt;ItemTemplate&gt;
                                         &lt;asp:Label ID="choice2" runat="server" Text='&lt;%# Bind("choice2") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;                                                                             
                          &lt;/asp:TemplateField&gt; 
                           &lt;asp:TemplateField HeaderText="Choice 3:"&gt;           
                          &lt;ItemTemplate&gt;
                                         &lt;asp:Label ID="choice3" runat="server" Text='&lt;%# Bind("choice3") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;                                                                             
                          &lt;/asp:TemplateField&gt; 
                           &lt;asp:TemplateField HeaderText="Choice 4:"&gt;           
                          &lt;ItemTemplate&gt;
                                         &lt;asp:Label ID="choice4" runat="server" Text='&lt;%# Bind("choice4") %&gt;'&gt;&lt;/asp:Label&gt;
                          &lt;/ItemTemplate&gt;                                                                             
                          &lt;/asp:TemplateField&gt;
                  &lt;/Fields&gt;
                  &lt;HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
                  &lt;EditRowStyle BackColor="#999999" /&gt;
                  &lt;AlternatingRowStyle BackColor="White" CssClass="generaltext" 
                      ForeColor="#284775" /&gt;
              &lt;/asp:DetailsView&gt;
  
      Your Answer:&amp;nbsp;
              &lt;asp:DropDownList ID="answerDropDownList" runat="server" 
                  style="margin-bottom: 0px"&gt;
                  &lt;asp:ListItem Value="1"&gt;Answer 1&lt;/asp:ListItem&gt;
                  &lt;asp:ListItem Value="2"&gt;Answer 2&lt;/asp:ListItem&gt;
                  &lt;asp:ListItem Value="3"&gt;Answer 3&lt;/asp:ListItem&gt;
                  &lt;asp:ListItem Value="4"&gt;Answer 4&lt;/asp:ListItem&gt;
              &lt;/asp:DropDownList&gt;
  
    &lt;asp:Button ID="buttonNext" runat="server" Text="Next" /&gt;
  </code></pre>
  
  <hr>
  
  <p>QuizPage.aspx.vb</p>
  
  <pre><code>Private Function CreateConnection() As SqlConnection
      Dim _connectionString As String = ConfigurationManager.ConnectionStrings("LMSConnectionString").ConnectionString
      Return New SqlConnection(_connectionString)
  End Function
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      If Not IsPostBack Then
          getQuestions()
      End If
  End Sub
  Private Sub getQuestions()
      Dim quiz_id As Integer
      quiz_id = Session("quiz_id")
      Dim connection As SqlConnection = CreateConnection()
      Dim command As SqlCommand = Nothing
      Dim dt As DataTable = New DataTable()
      command = New SqlCommand("SELECT question_id,quiz_question, choice1, choice2, choice3, choice4, answer, quiz_id FROM tblQuizQuestion WHERE (quiz_id = @quiz_id)", connection)
      command.Parameters.AddWithValue("@quiz_id", quiz_id)
      Dim ad As SqlDataAdapter = New SqlDataAdapter(command)
      ad.Fill(dt)
      questionDetails.DataSource = dt
      questionDetails.DataBind()
  End Sub
  Protected Sub buttonNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonNext.Click
  
      Try
  
          ' Save off previous answers
          Dim dr As System.Data.DataRowView
          dr = CType(questionDetails.DataItem, System.Data.DataRowView)
  
          ' Create Answer object to save values
          Dim a As Answer = New Answer()
          ' a.QuestionID = dr("QuestionOrder").ToString()
          a.CorrectAnswer = dr("answer").ToString()
          a.UserAnswer = answerDropDownList.SelectedValue.ToString()
  
          Dim al As ArrayList
          al = CType(Session("AnswerList"), ArrayList)
          al.Add(a)
  
          Session.Add("AnswerList", al)
  
      Catch ex As Exception
  
  
          Response.Redirect("default.aspx")
      End Try
  
      If questionDetails.PageIndex = questionDetails.PageCount - 1 Then
          ' Go to evaluate answers
          Response.Redirect("results.aspx")
      Else
          questionDetails.PageIndex += 1
  
      End If
  
      If questionDetails.PageIndex = questionDetails.PageCount - 1 Then
          buttonNext.Text = "Finished"
      End If
  End Sub
  </code></pre>