Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
hi! i am new in vb.net programming.i am making this simple test application and i would like to know how can i retrieve data from database randomly.this is my sql server database table imgage. http://www.photoshop.com/users/akosibogart/albums/1770ea40419f4176a14c0f055807270e/view#e9c1e825e5394da5afef1c5699f848fb[^]
 
VB form: http://www.photoshop.com/users/akosibogart/albums/1770ea40419f4176a14c0f055807270e/view#c5fad573b3c04b85bb3858ab122be112[^]
 

Problem and Question: The user will click generate question button to retrieve a data randomly from the table questionnaire.Next, the user will type the correct answer from the textbox "Type your answer here" then check button will check if AnswerTextbox is equal to textbox1 which is the input your answer here textbox. Im using databinding in this coding method.hope u can help me. im noob in this programming language.any idea on how can i do this?
Posted 14-Jan-13 0:47am
Edited 14-Jan-13 12:51pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Provided your ID values are contiguous and you know teh max and min values, the easiest way is you to retrieve a recode with a random ID between those two values. SQL doesn't have an concept of "a random row" so it is a lot easier to do that in your presentation layer. At the same time, you can keep a record of which questions you have already asked a particular user, so that you don't ask it again...
  Permalink  
Comments
akosibogart at 14-Jan-13 7:17am
   
i set the identity specification of the questionID to "yes" so meaning sql will give a unique ID whenever a new data is added to the table. in the button generatequestion i want to retrieve a data randomly without repeating.any idea on how can i do it the easiest way?
OriginalGriff at 14-Jan-13 7:29am
   
You can retrieve the max and min values of the ID column from the table and then generate a random value between them.
SELECT MAX(id) as Max, MIN(id) as Min FROM MyTable
Identity specification integers always increase by the "Identity Increment" value, so provided you leave this at the default of 1 thewr will be no holes unless you start deleting questions.
akosibogart at 14-Jan-13 7:44am
   
sir, i cant fully understand what you are telling me right now because im already noob to this programming. can u send me your code from the generate button and i will try to understand what your trying to say.. plss, plss..
OriginalGriff at 14-Jan-13 7:52am
   
No, I'm not going to do your homework for you! :laugh:
You will get the credit, so it's only fair you should do some of the work...don't you agree?
If you use the above SELECT command in your normal SQL access code, you will retrieve the max and min values.
You can then use these with a Random class instance to generate a random question ID.
You can then read that question only from the database.
 
(or, you could read them all and pick one at random, but that is less efficient in the long run)
 
Which part of this is difficult for you?
akosibogart at 14-Jan-13 8:38am
   
how sad..anyway sir tnx for your tym. ill try to find some tutorials to answer my question easily.the problem is that i cant easily understand a solution to a problem through reading.
OriginalGriff at 14-Jan-13 9:23am
   
As I said: "Which part of this is difficult for you?"
We are willing to help, but not to do it all for you.
akosibogart at 14-Jan-13 8:43am
   
its to hard to understand from a noob like me to a lord programmer like you..thank you again sir for your help.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Please check following link.
http://www.sqlteam.com/article/using-newid-to-randomly-sort-records[^]
 
When the user first accessing to the system, you have to setup the randomize question list for him (you can use the table (userid, questionid)) for that. Then you can retrieve question 1 by 1 from that table and display to the user.
  Permalink  
Comments
akosibogart at 14-Jan-13 9:11am
   
sir ,i am new in vb a very noob. as u can see in my post.do u think it should answer my question? ill try ur link..
Tharaka MTR at 14-Jan-13 9:14am
   
ok. great.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

tNx experts now i know.. my test form is now working.
  Permalink  
v4
Comments
Sergey Alexandrovich Kryukov at 18-May-13 5:52am
   
Not an answer, does not belong here.
—SA
akosibogart at 18-May-13 7:42am
   
?
Sergey Alexandrovich Kryukov at 18-May-13 21:50pm
   
Please don't post non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership. And the fact you even self-accepted some formally is just outrageous, a sure way for a ban. I hope you won't do it after this warning.
 
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 315
1 Sergey Alexandrovich Kryukov 201
2 ChauhanAjay 156
3 PhilLenoir 154
4 ClimerChinna 140
0 Sergey Alexandrovich Kryukov 6,140
1 OriginalGriff 5,195
2 CPallini 2,473
3 Richard MacCutchan 1,607
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100