Solved

Using a DataTable to hold randomly generated values

Posted on 2004-08-09
9
242 Views
Last Modified: 2010-04-17
Hi

I have developed a quiz that randomly generates questions from a bank of stored questions. Firstly, how can I make sure that each randomly generated question is unique. eg I dont want it to ask the same question more than once.

I had an Idea to store the selected questions in a temporary datatable and then at the end of the quiz to populate the selected questions to the database.

I realise this is quite a lot to ask and therefore would be very gratefull for appropriate feedback.

Thanks very much in advance
0
Comment
Question by:pgilfeather
9 Comments
 
LVL 4

Expert Comment

by:alikoank
Comment Utility
What language and database are you using?

You can use temporary tables, or keep used questions in a list structure.
0
 
LVL 4

Expert Comment

by:Pichto
Comment Utility
Hi khurram007,

You are right... you have to store the questions you've already asked somewhere. Then, when you select a new random question, just check if you alredy have it in your list of  "already-asked-question" and, if yes, select another one...

Now, you can choose whatever you want to hold your values : datatable, array that you store in session, text file, cookie, xml file, ...


HTH

Pichto
0
 

Author Comment

by:pgilfeather
Comment Utility
Sorry, I am using VB.NET with SQL Server

I have had it working before, using a listbox to hold the generated questions but I dont know how to check the listbox for the values ie what property/method is used to check a value exists in a listbox? And even if I get this far can I populate the database with the values stored in the listbox?

DataSet information is very hard come by. Where can I get particularly good information on storing datasets in session, xml files etc...?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 4

Accepted Solution

by:
alikoank earned 250 total points
Comment Utility
you can use temporary tables.

any table that begins with an # character is temporary in SQL server. They are automatically dropped when you end your database session.

You can modify your SQL select statements to something like this:

SELECT question from ALL_QUESTIONS
where question not in (select question from #ASKED_QUESTIONS)

do not forget to create #ASKED_QUESTIONS on each new session and put the questions you asked inside it.
0
 
LVL 3

Expert Comment

by:aravindtj
Comment Utility
Hai,
 it is very simple.
 You know the total number of questions N.
 And you are having the Questions in your database [ table/file/array....]
 Generate N unique random numbers like following algorithm:

array qnlist(N);
i=1;
while(i<= N)
{
   temp = (ran*1000) mod N;
   for(j=i to N)
   {
       if(qnlist(i) == qn(j))
             continue;// for loop i
   }
   qnlist(i)=temp;
   i = i+1;
}

[ Omit the syntax. just go throw the algorithm]

 Using this array, in where the random qn numbers are there, access the corresponding qns from the database, which you have already.
 This may helpful to you.
All the best.
0
 

Author Comment

by:pgilfeather
Comment Utility
aravindtj,

Sorry, but I only know VB.NET with SQL Server
0
 
LVL 1

Assisted Solution

by:keijerjh
keijerjh earned 250 total points
Comment Utility
Hi,

Maybe this is the idea.
Load a couple a questions (See source GetQuestions) into a ADO.NET DataTable and add two columns ANWER and QUESTION_ANSWERED.
Than loop trough the rows (questions) and set the columns ANSWER and QUESTION_ANSWERED.
Than save the answers to the database (See source PutAnswers).

I hope this is the idea you are looking for or it will help you.
Good luck.

Greeting Yelle.

*******************************************************************************
Private Function GetQuestions As Data.DataTable
Dim oConnection As Data.OleDb.OleDbConnection
Dim oCommand As Data.Ole.OleDbCommand
Dim oDataReader As Data.OleDb.OleDbDataAdapter
Dim oDataTable As Data.DataTable
Dim oDataRow As Data.DataRow

oDataTable = New Data.DataTable
With oDataTable
      .Columns.Add("QUESTION")
      .Columns.Add("ANSWER")
      .Columns.Add("QUESTION_ANSWERED",Boolean.GetType)
End With
oConnection = New Data.OleDb.OleDbConnection(YourConnectionString)
oConnection.Open
oCommand = New Data.Ole.OldbCommand
With oCommand
      .Connection = oConnection
      .CommandText = "SELECT QUESTION FROM QUESTIONS"
End With
oDataReader = oCommand.ExecuteReader
While oDataReader.Peek <> - 1
      oDataRow = oDataTable.NewRow
      oDataRow("QUESTION") = oDataReader("QUESTION")
      oDataRow("ANSWER") = ""
      oDataRow("QUESTION_ANSWERED") = False
      oDataTable.Rows.Add(oDataRow)
Wend
oDataReader.Close
oDataReader = Nothing
oCommand = Nothing
oConnection.Close
oConnection = Nothing
Return oDataTable
End Function

Private Function PutQuestions(DataTable As Data.DataTable)
Dim oConnection As Data.OleDb.OleDbConnection
Dim oCommand As Data.Ole.OleDbCommand
Dim oDataTable As Data.DataTable
Dim oDataRow As Data.DataRow

oConnection = New Data.OleDb.OleDbConnection(YourConnectionString)
oConnection.Open
oCommand = New Data.Ole.OldbCommand
With oCommand
      .Connection = oConnection
      .CommandText = "INSERT INTO QUESTION_ANSWERS (QUESTION,ANSWER) VALUES (?,?)"
      .Parameters.Add(“QUESTION”)
      .Parameters.Add(“ANSWER”)
End With
      For Each oDataRow In oDataTable.Select(“QUESTION_ANSWERED=True”)
            With oCommand
                  .Parameters(“QUESTION”).Value = oDataRow(“QUESTION”)
                  .Parameters(“ANSWER”).Value = oDataRow(“ANSWER”)
                  .ExecuteNonQuery
            End With
      Next
oCommand = Nothing
oConnection.Close
oConnection = Nothing
End Function
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now