Click here to Skip to main content
13,300,002 members (64,194 online)
Rate this:
Please Sign up or sign in to vote.
hi! i am new in 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.[^]

VB form:[^]

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 1:47am
Updated 14-Jan-13 13:51pm
Rate this: bad
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...
akosibogart 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 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 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 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 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 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 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
Please Sign up or sign in to vote.

Solution 2

Please check following link.[^]

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.
akosibogart 14-Jan-13 9:11am
sir ,i am new in vb a very noob. as u can see in my u think it should answer my question? ill try ur link..
Tharaka MTR 14-Jan-13 9:14am
ok. great.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

tNx experts now i know.. my test form is now working.
Not an answer, does not belong here.
akosibogart 18-May-13 7:42am
Sergey Alexandrovich Kryukov 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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2017
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