Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 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
Updated 14-Jan-13 12:51pm
v3

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...
 
Share this answer
 
Comments
akosisugar 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.
akosisugar 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?
akosisugar 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.
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.
 
Share this answer
 
Comments
akosisugar 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 14-Jan-13 9:14am    
ok. great.
tNx experts now i know.. my test form is now working.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 18-May-13 5:52am    
Not an answer, does not belong here.
—SA
akosisugar 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.
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900