Hi guys,
i used the query for randomly selecting questions from access but it will give me same result, i try it by passing different different values also passing time in rnd fuction but i will display same result.

DataSet ds = new DataSet();
            string strsel = "select * from starttest where stud_id='101301' order by rnd("+ kk +")";
            Oda = new OleDbDataAdapter(strsel, Connect.AccConn());
            Oda.Fill(ds, "starttest_new");
            DataTable dt = ds.Tables[0];
            Oda.Update(dt);
            dataGridView2.DataSource = dt;

can anybody help me....
Thanks in advance

Recommended Answers

All 6 Replies

What is kk? Instead of using a passed constant, try using an ID field in rnd():

Say you have a primary key called ID in this table:

string strsel = "select * from starttest where stud_id='101301' order by rnd([ID])"

According to this passing a constant to rnd() will generate the same results everytime, but passing a numeric column(such as an auto-incrementing ID) will ensure that rnd() gets regenerated for each record.

Hi skatamatic, even through i pass primary key it will display same result. Even i try date.Now.Second but still not succeed.

Hmmm. Can you post the new query? Maybe try multiplying the current date.now.millisecond with the primary key. This way the random value is dependant on both the PK and the current time.

Here is my new query,

string strsel = "select * from starttest where stud_id='101301' order by rnd([que_id]), rnd("+ DateTime.Now .Second +")";

Are you sure que_id is an auto int pk? And you should try multiplying the que_id by the time insid of rnd:

string strsel = "select * from starttest where stud_id='101301' order by rnd([que_id]*Second(Now()))";

I tested it and it works on my machine (with a different database obviously).

Hi skatamatic,
please check my code and tell me where i am going wrong

dataGridView1.Rows.Clear();
            DataSet ds = new DataSet();
          
            string strsel = "select * from starttest where stud_id='101301' order by rnd([que_id]*Second(Now()))";
            Oda = new OleDbDataAdapter(strsel, Connect.AccConn());
            Oda.Fill(ds, "starttest_new");
            DataTable dt = ds.Tables[0];
            Oda.Update(dt);
            dataGridView2.DataSource = dt;
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.