random selection of database entries

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 8
Reputation: ariez88 is an unknown quantity at this point 
Solved Threads: 0
ariez88 ariez88 is offline Offline
Newbie Poster

random selection of database entries

 
0
  #1
Jul 8th, 2009
hello,
can anyone tell me how to randomly pick entries from a database using ASP.NET as front end and SQL SERVER as backend ?

i have written
select * from table order by rand();

but instead of picking entries randomly, it simply displays the entries in the same order as they are stored in the database..

thanks alot.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 439
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training

Re: random selection of database entries

 
0
  #2
Jul 8th, 2009
Try this
  1. SELECT TOP 100 *
  2. FROM Your_Table_Name
  3. ORDER BY NEWID()
This will pick 100 records randomly from a SQL Server 2005 table.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 8
Reputation: ariez88 is an unknown quantity at this point 
Solved Threads: 0
ariez88 ariez88 is offline Offline
Newbie Poster

Re: random selection of database entries

 
0
  #3
Jul 9th, 2009
thankyou very much.. there seems to be a problem with SQL at my home so i was unable to try it yet..I'll try it in the uni the next time i go there.
another problem tht i m having is in this code..


  1. protected void loginButton_Click1(object sender, EventArgs e)
  2. {
  3. DataSet temp = new DataSet();
  4. temp = conn.retrieve("select * from candidate_info");
  5. int i=0;
  6. int a = temp.Tables[0].Rows.Count;
  7. //object obj = new object();
  8.  
  9. object[] obj;
  10.  
  11. while( i < a)
  12. {
  13. obj = temp.Tables[0].Rows[i].ItemArray;
  14.  
  15. if (this.idTextBox.Text != obj[0].ToString() && this.passTextBox.Text != obj[1].ToString())
  16. i++;
  17.  
  18. }
  19. if (i == a)
  20. Response.Write("<script>alert('ERROR')</script>");
  21. else
  22.  
  23. Response.Write("<script>alert('Logged In')</script>");
  24.  
  25. }

this is the piece of code tht I have written for the sign in button. The problem here is tht whenever I input wrong entry it gives error in logging in.. Which is correct but when ever I enter an entry tht is present in the database it is stuck there and never goes to the next page and never shows the “logged in ” message. and sometimes it even shows the logged in message for wrong entries... i dont know how to handle it..i guess there is some mistake in the brackets..
can anyone help me??
thanks alot
Last edited by peter_budo; Jul 10th, 2009 at 6:24 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 164
Reputation: dnanetwork has a little shameless behaviour in the past 
Solved Threads: 24
dnanetwork's Avatar
dnanetwork dnanetwork is offline Offline
Junior Poster

Re: random selection of database entries

 
0
  #4
Jul 10th, 2009
grab all the value from the database by using the select statment & next store it in the session array or arraylist and next to acces them use random class provided by C#...
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 8
Reputation: ariez88 is an unknown quantity at this point 
Solved Threads: 0
ariez88 ariez88 is offline Offline
Newbie Poster

Re: random selection of database entries

 
0
  #5
Jul 11th, 2009
I have tried this. please have a look at it and kindly let me know....
Errors are written within the code. and will you please explain how to access them using random class?



  1. protected void loginButton_Click1(object sender, EventArgs e)
  2. {
  3. ArrayList arr = new ArrayList();
  4. arr =conn.retrieve("select * from candidate_info");
  5.  
  6. /*
  7.  
  8.   Error:Cannot implicitly convert type
  9.  
  10.   'System.Data.DataSet' to 'System.Collections.ArrayList'
  11.  
  12.   */
  13.  
  14. int i=0;
  15. int a = arr.Count;
  16. Random rand = new Random();
  17.  
  18. // where to use this random class. what will it do ???
  19.  
  20. while (i < a)
  21.  
  22. {
  23.  
  24. // rand = arr[0]; confused here ................
  25.  
  26. if (this.idTextBox.Text != obj[0].ToString() && this.passTextBox.Text != obj[1].ToString())
  27.  
  28. i++;
  29.  
  30. }
  31.  
  32. if(i == a)
  33.  
  34. Response.Write("<script>alert('ERROR')</script>");
  35.  
  36. else Response.Write("<script>alert('Logged In')</script>");
  37. }

many thanks..
Last edited by peter_budo; Jul 11th, 2009 at 2:38 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 217
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 32
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training

Re: random selection of database entries

 
0
  #6
Jul 13th, 2009
Try to modify your code segment in the following way. If matched then exit other wise after completion of all iteration its sure that no matched data found means error.

  1. while (i < a)
  2.  
  3. {
  4. // rand = arr[0]; confused here ................
  5. // Use trim for fixed lenth db datattype if varchar then leave it
  6. if (this.idTextBox.Text == obj[0].ToString().Trim() && this.passTextBox.Text == obj[1].ToString().Trim())
  7. {
  8. Response.Write("<script>alert('Logged In')</script>");
  9. return;
  10. }
  11. i++;
  12. }
  13.  
  14. Response.Write("<script>alert('ERROR')</script>");
Last edited by mail2saion; Jul 13th, 2009 at 1:15 am.
MARK AS SOLVED if its help you.

REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC