Improve company productivity with a Business Account.Sign Up

x
?
Solved

Using a DataTable to hold randomly generated values

Posted on 2004-08-09
9
Medium Priority
?
254 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
7 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 Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for 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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Make the most of your online learning experience.
Simple Linear Regression
Progress

586 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