Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using a DataTable to hold randomly generated values

Posted on 2004-08-09
9
Medium Priority
?
251 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
ID: 11751107
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
ID: 11751114
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
ID: 11751167
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
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.

 
LVL 4

Accepted Solution

by:
alikoank earned 1000 total points
ID: 11751190
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
ID: 11754476
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
ID: 11760871
aravindtj,

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

Assisted Solution

by:keijerjh
keijerjh earned 1000 total points
ID: 11824760
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Starting up a Project

916 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