Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir i design exam site but now problem is how to get random question from database in always different sequence on aspx page
Posted

Select Random Question From Your Table :

Use NEWID()

SQL
select top 1 column from Table order by NEWID()
 
Share this answer
 
v2
Since you're going to work with Random questions, and you already have the basics I would like to forward you to MSDN for more details, but will give you the basic idea of implementation.

You can create a list of the questions from the database, and the List being another kind of array will let you get a question from it using the indexers, (eg, myList[0]; get the element at 1st index). Just to make sure that these are captured randomly, .NET would let you use a Random object to select randomness. The class Random[^] lets you do this.

You can create an instance of this class, and use it, to create a Random number on the run-time, to use that number to select the element (which is a string containing the question) from the list (which, when you're going to query the database will be returned as IEnumerable<object></object>) object.

The sample code for this would be like this,

C#
// create index within 0-99
var index = new Random().Next(100);
// assume, questions is an outcome of db.Query(string); method, then
var question = questions[index];
// now the question will contain the question object from the list.


.. you can then use it inside the HTML to render it like this,

HTML
<div>
   <h3>@question.Title</h3>
   <p>@question.Description</p>
</div>


.. this will do the thing for you. For more on this, and creating the Random numbers, do read that MSDN document attached.
 
Share this answer
 

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