Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET help to add random questions to the data table

Posted on 2009-05-06
2
Medium Priority
?
356 Views
Last Modified: 2012-06-27
Hello Experts,

I am having situation where I need to add 30 random question to the temp table (data table) i am trying to make at the run time....

I have a script already to show random questions one at a time on the page, every time you click on the next button you get the next random question from the total 100 questions.

All I am trying here is to use the same script but instead of the click event I would have to use a For Loop to return 30 random questions and assign them to the table...

I hope you may understand If you could please see the attached code snippet.

I will be glad if someone of you may advise me the correct form to fetch and assign them to the temp table.

Once I have the data ready I can then send the dynamic table to the crystal report to produce the report in the desired output format, ex: PDF.

Your help would greatly be appreciated.

Thanks
S

0
Comment
Question by:newbie27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Author Comment

by:newbie27
ID: 24320797
button_onclick
==================

               Dim NextQuestion As Integer = get_Next_Question() 'CHANGED
      Dim TestID as String = 1
                ShowQuestion(NextQuestion, TestID)


However I want to call the showquestion and option function in the following function

 Private Function BuildGiftCodeTable(ByVal GiftCodeType As String, ByVal NumCodes As Integer, ByVal GiftTypeID As WebFunctions.GiftType) As DataSet
        Dim ds As New DataSet("GiftCodes")
        Dim dt As DataTable
        Dim dr As DataRow
        Dim CodeStart, GiftCodeID As String
        Const GiftCodeIDLength As Integer = 14
        Dim CodeLength As Integer
        Dim ExitLoop As Boolean = True

        'define the structure of the table
        dt = New DataTable("GiftCode")
        dt.Columns.Add("GiftCodeID")
        dt.Columns.Add("ExpiryDate")


       
        'Get the first characters of the code
        CodeStart = GiftCertificateTypeBLL.GetTypeGiftCode(GiftCodeType)

        'Create a gift code for each number of codes to be created
        For i As Integer = 1 To NumCodes
            Do
                CodeLength = GiftCodeIDLength - Len(CodeStart)
                GiftCodeID = CodeStart & GenerateGiftCode(CodeLength)

                'Check whether the GiftCodeID already exists
                ExitLoop = True
                If GiftCertificateBLL.GetGiftCertificate(GiftCodeID).Rows.Count = 0 Then
                    'Search the codes that were just created
                    For Each Row As DataRow In dt.Rows
                        If GiftCodeID = Row.Item("GiftCodeID") Then
                            ExitLoop = False
                        End If
                    Next
                    If ExitLoop Then
                        Exit Do
                    End If
                End If
            Loop

            dr = dt.NewRow
            dr.Item("GiftCodeID") = GiftCodeID
            dr.Item("ExpiryDate") = GetExpiryDate(WebFunctions.GiftType.NameAStar)
            'Dim GiftTypeID As String = "CHOCOLATE"

            If GiftTypeID = WebFunctions.GiftType.MasterTheArtOf Then
                dt.Columns.Add("TestName")
                dr.Item("TestName") = LearnItClass.GetTestName(GiftCodeID)

''''' ===============   something like this here  ============================
                dt.Columns.Add("Question" & 1)
                dr.Item("Question" & 1) = ""

                dt.Columds.Add("Option" & 1)
                dt.Item("Option" & 1) = ""

                dt.Columds.Add("Option" & 2)
                dt.Item("Option" & 2) = ""

                dt.Columds.Add("Option" & 3)
                dt.Item("Option" & 3) = ""

                dt.Columds.Add("Option" & 4)
                dt.Item("Option" & 4) = ""
''''' ===============   something like this here  ============================

''''' ===============  this should pick 30 random questions and options==================
            End If
            dt.Rows.Add(dr)
        Next


        'add the table to the dataset
        ds.Tables.Add(dt)
 
        Return ds
    End Function

===============================

I hope someone may understand what I am trying to say here?

Thanks for any help.

Regards
S
 Protected Sub ShowQuestion(ByVal QuestionNumber As Integer, ByVal sTestId As String)
        Dim QId As Integer
        Try
            SqlDataSource.SelectCommand = "SELECT * FROM LearnItQuestions WHERE QuestionNumber = '" & QuestionNumber & "' AND TestID = '" & sTestId & "'"
            Dim DV As DataView = CType(SqlDataSource.Select(DataSourceSelectArguments.Empty), DataView)
            If (DV IsNot Nothing) And (DV.Count > 0) Then
                lblQuestions.Visible = True
                lblQuestions.Text = DV(0)("QuestionContent").ToString()
                QId = DV(0)("QuestionId")
            End If
            hidQuestionID.Value = QId
            ShowOptions(QId)
        Catch ex As Exception
            'lblQuestions.Text = "Problem Retrieving Questions, Please Contact the Administrator." & ex.ToString()
            GiftRepublic.eMail.SendErrorEmail("Problem showing the questions..", ex.ToString)
        End Try
 
 
    End Sub
    
    Protected Sub ShowOptions(ByVal QuestionID As Integer)
        Try
            SqlDataSource.SelectCommand = "SELECT * FROM LearnItOptions WHERE QuestionID = '" & QuestionID & "'"
            Dim DV As DataView = CType(SqlDataSource.Select(DataSourceSelectArguments.Empty), DataView)
            Dim MultipleOptions As Integer
            rdoMultipleOptions.Items.Clear()
            For MultipleOptions = 0 To 3 Step 1
                rdoMultipleOptions.Items.Add(DV(MultipleOptions)("Option").ToString())
            Next
 
        Catch ex As Exception
            ' lblQuestions.Text = "Problem Retrieving Multiple Choice Options, Please Contact the Administrator." & ex.ToString()
            GiftRepublic.eMail.SendErrorEmail("Problem Retrieving Multiple Choice Options.", ex.ToString)
        End Try
 
    End Sub
 
 
Protected Function get_Next_Question()
 
        Dim UpperBound As Integer = 50
        Dim LowerBound As Integer = 1
 
        'x = RandomNumbers(50, 1, 1)
 
        Dim NextQuestion As Integer
        If is_Complete() = False Then
            Randomize()
            NextQuestion = CInt(Int((UpperBound - LowerBound + 1) * Rnd() + LowerBound))
            'NextQuestion = RandomNumbers(50, 1, 1)
            While (is_Already_Answered(GetQuestionIDByNumber(NextQuestion)) = True)
                Randomize()
                NextQuestion = CInt(Int((UpperBound - LowerBound + 1) * Rnd() + LowerBound))
                'NextQuestion = RandomNumbers(50, 1, 1)
            End While
            ' NextQuestion = GetRandomQuestion()
 
        Else
            SessionData.TestStatus = "Complete"
            Response.Redirect("EndTest.aspx")
        End If
        Return NextQuestion
 
 
 
    End Function

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 24325094
You would use this line

Dim NextQuestion As Integer
for i as integer = 0 to 29
     NextQuestion =  get_Next_Question()
     'Write code to retrieve the question from the database same as being done in the showquestion method
     'Write code to add the retrieved question to the datatable for example:
     dRow = dQuestions.NewRow
     dRow(0)=datareader.item(0)
     dRow(1) = datareader.item(1)
     ...
     dQuestions.Rows.Add(dRow)
next
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question