Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
New to DataBase. I have a sql server database with one datatable connected to my application. I want the rows of this datatable to be sorted randomly each time the application starts. How do I do that?

[Edit - further detail from OP]
I'm trying to make a quiz. My datatable has four colomns (the first holds a question and the other three hold the answers). So I want the questions to appear in random order each time the game starts. I tried to use bindingsource.sort but it didn't work
Posted
Updated 8-May-13 5:12am
v2
Comments
CHill60 8-May-13 10:32am    
"sorted randomly" is an oxymoron - either they come at you in a random fashion or they are sorted - can't be both. What are you actually trying to achieve?
al3abby 8-May-13 11:00am    
I'm trying to make a quiz. My datatable has four colomns (the first holds a question and the other three hold the answers). So I want the questions to appear in random order each time the game starts. I tried to use bindingsource.sort but it didn't work
CHill60 8-May-13 11:10am    
Ah - now I understand ... I'll get back to you with some thoughts in a moment

Try reading the data with the following SQL
SQL
SELECT TOP 100 percent * FROM [yourtable] ORDER BY newid()

You can change the 100 percent - I just assumed you'd want the entire table.
You could also drop the percent to get e.g. 100 questions.

With acknowledgements to Ralph Shillington[^]
 
Share this answer
 
Comments
Thomas Daniels 8-May-13 11:27am    
+5!
CHill60 8-May-13 11:37am    
How are you getting your questions out of the database at the moment?
al3abby 8-May-13 11:52am    
After I created the database using sql server management studio, I added it to my project from add new item. Then from the Data Sources window I added a new data source (my table) and I chose the datagridview from the dropdown box and I draged it to my form. Now the questions appear in the order in which I entered them.
CHill60 8-May-13 12:05pm    
Ok - you might be better off using a sqldataadaptor to get your data (in code) - you can load the table up into a dataset (using the sql in my solution) and bind that dataset to the datagridview - e.g. in your form load event
al3abby 9-May-13 17:02pm    
I got it, thanks
My first thought is that you add a column. For each row you generate a random number and populate that column with it. Then sort by that column.

Google Results for Generating a Random Number[^]
 
Share this answer
 
Comments
al3abby 8-May-13 11:19am    
Thank you Kschuler, I tried that. I wouldn't say it didn't work, but I didn't know how to do it.
Kschuler 8-May-13 11:21am    
you tried it, but you didn't know how to do it? What about it were you having trouble with?
Maciej Los 8-May-13 14:44pm    
+5

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