Solved

Using a DataTable to hold randomly generated values

Posted on 2004-08-09
9
248 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
[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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
alikoank earned 250 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 250 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Simple Linear Regression
Starting up a Project

707 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