Hey I am trying to do a project on Test Engine. I need to retrieve random rows of questions from access database. I did the following code, but every time I once close and run it again, the sequence of questions is same. How can I change the sequence every time?

private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection(Connection.conStr);
            OleDbCommand cmd = new OleDbCommand("select * from dell order by Rnd(serialNumber)", con);
            OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds, "new");
            dataGridView1.DataSource = ds.Tables["new"];
        }

Recommended Answers

All 5 Replies

SELECT *, Rnd(serialNumber) FROM dell ORDER BY Rnd(serialNumber)

check this link it contains a detailed discussion on how to randomly select values

Use any of these Queries

SELECT *, rand(@@IDLE % (@@ROWCOUNT + serialNumber))as xx FROM dell ORDER BY xx
//or
SELECT *,(sin(serialNumber * rand())) as xx from dell
ORDER BY xx

@abelLazm Thanks a lot.. It worked..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.