?
Solved

Using a DataTable to hold randomly generated values

Posted on 2004-08-09
9
Medium Priority
?
249 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
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.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

777 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