Hi Guys,

Can you have any idea about randomly shuffle the questions in database without affecting questions id.

Let i will explain little bit more ...... read carefully
1) I have a table containing 5 questions like this

que_id que

1 abc
2 bcd
3 efg
4 hij
5 klm

2)I am using access database, now in my application their are multiple user suppose two users 1,2
3) Now what i want when every time user has started the test he will find questions should be shuffled not on next button remember. Question shuffled on first time only.
4) But remember how to manage these things on next and previous button. For that actually i have added a one column in table which is auto number after shuffling i am fetching next que_id on the basis of that auto number but doesn't work

Hope you guys understand my problem........ so waiting for Ur's pleasant reply.

Why are you trying to shuffle in the database? Extract the questions and ID into a dataTable and randomly select which ID will be next (or shuffle if you want the order to be established before the test begins). You could easily record which question was the previous one using an array or arrayList.

Hi hericles,
can u provide some code regarding this..actually i am not much worked with datatable.

you can do this using the Random Class. Just create one Random class
count your datatable rows and then looping on count
put that rows into another datatable.
for Ex.

Private Datatable ShuffelTable()
	DataTable Table = mytable.copy();
	Random _Shuffelrandom = new Random();
		while (mytable.Rows.Count > 0)
		int Row=_Shuffelrandom.Next(i,Table.Rows.Count);
	return Table;

mytable is it datatable?

but from that code

int i=0;

and i will increment in loop right but it throws an error like their is no row at position 0.


Here it

Edited 4 Years Ago by bhagawatshinde: n/a

These guys are right. It makes more sense to do the processing of the data in c# than in Access.

LINQ is very powerful for this (non implemented logic that you might find useful):

//Get the data
            DataTable myData = LoadDataFromAccess();

            //Scramble the data and build some objects with it
            //then maybe store this in memory as a member to avoid the DB overhead
            //when you want to make a new list of questions and answers
            var myQuestionsAndAnswers = (from s in myData.AsEnumerable()
                                  select new 
                                      myQuestion = s.Field<string>("Question"),
                                      myAnswer = s.Field<string>("Answer")
                                  .ToDictionary(q => q.myQuestion,a => a.myAnswer);
            //Build a scrambled list that you can use for your questions and answers that contains the number of
            //question/answer pairs that you want (assume QUESTION_COUNT is defined)
            Random rand = new Random();
            var myRandomList = myQuestionsAndAnswers
                             .OrderBy(z => rand.Next())
            //To display the scrambled questions and answers, though you probably want the question
            //to appear and have the user enter the answer then compare it with the actual answer
            //I'll let you figuire this part out...
            myRandomList.ForEach(z => 
                Console.WriteLine(string.Format("Question: {0}   Answer: {1}", z.Key, z.Value)));

Edited 4 Years Ago by skatamatic: n/a

All LINQ can be represented by iterations:

//Get the data
            DataTable myData = LoadDataFromAccess();

            //Create a dictionary to hold the questions and answers
            Dictionary<string, string> myQandA = new Dictionary<string, string>();
            //Fill it with the corresponding columns from the datatable
            foreach (DataRow r in myData.Rows)
                myQandA.Add(r["QuestionCol"].ToString(), r["AnswerCol"].ToString());

            //Build a list of these key value pairs to hold some random question/answer pairs
            List<string> myRandomQuestions = new List<string>();
            //Build a temp list of myQandA keys to randomize
            List<string> myRandomKeys = new List<string>();
            //Add all the keys from the myQandA dictionary to this list
            foreach (string s in myQandA.Keys)

            //Set up a randomizing loop
            Random r = new Random();
            int iRand = 0;
            int iCount = 0;
            //Stop adding questions if the count is exceeded or you run out of questions
            while (iCount++ < QUESTION_COUNT || myRandomKeys.Count == 0)
                //Get a random int, with the max being the total available q/a pairs
                iRand = r.Next(myRandomKeys.Count);
                //Add the question to the list from a random index
                //Remove the added question so it doesn't get duplicated
            //Present the data to the user
            foreach (string s in myRandomQuestions)
                Console.Write(string.Format("Question: {0} -> Answer: {1}", s, myQandA[s]));

LINQ is probably a lot more optimized than this though. Especially in the shuffling part. I would sugguest updating your development computer :)

Edited 4 Years Ago by skatamatic: n/a

This question has already been answered. Start a new discussion instead.