Go4Expert

Go4Expert (http://www.go4expert.com/)
-   MS Access (http://www.go4expert.com/forums/ms-access-forum/)
-   -   MS Access Query: Returning a random row problem (http://www.go4expert.com/forums/ms-access-query-returning-random-row-t5061/)

cjtemple 3Jul2007 01:03

MS Access Query: Returning a random row problem
 
I have a C# application that I am writing but everytime it runs the query it only returns the very first row. Any ideas what I am doing wrong?

Code:

        private void pickWinnerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataConnector dc = new DataConnector();
            try
            {
                string msg = "";
                Random rand = new Random();
                int seed = rand.Next();
                string query = "SELECT TOP 1 RND(" + seed + "), * FROM Members LEFT JOIN Winners ON Members.[MemberID] = Winners.[MemberID] WHERE (((Winners.MemberID) Is Null))";
                DataSet dsMembers = dc.GetMemberData(query, "Members");
                DataRowCollection drCollection = dsMembers.Tables["Members"].Rows;
                foreach (DataRow dr in drCollection)
                {
                    MessageBox.Show(query);
                    msg += dr["MemberFName"] + " " + dr["MemberLName"] + "\n" + dr["Address1"] + "\n" + dr["Address2"] + "\n" + dr["City"] + ", " + dr["State"] + " " + dr["ZipCode"] + "\nHome" + dr["PhoneH"] + "\nWork" + dr["PhoneW"] + "\nCell" + dr["PhoneC"];
                    MessageBox.Show("Congradulations to:\n\n" + msg, "Winner", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    dc.AddToDB("INSERT INTO Winners (MemberID) values(" + dr[1] + ")");
                }
            }
            catch (OleDbException ole)
            {
                ErrorBox("Query Error: " + ole);
            }
            catch (Exception ex)
            {
                ErrorBox("General Error: " + ex);
            }
        }

The purpose of the program is to pick a new winner from the table and to ensure that the individual has NOT already been choosen as a winner.

shabbir 3Jul2007 08:25

Re: MS Access Query: Returning a random row problem
 
TOP 1 means it will return the Top Row which is why its returning only one ROW.

cjtemple 3Jul2007 17:11

Re: MS Access Query: Returning a random row problem
 
Yes I only want 1 row, but I don't want it to always be the very first row in the database, I want it to be only 1 random row.

shabbir 3Jul2007 17:42

Re: MS Access Query: Returning a random row problem
 
Your random is not really random. See for multiple run and check whats the value of the seed.
Random rand = new Random();
int seed = rand.Next();


All times are GMT +5.5. The time now is 23:42.