Problem with Access Random Record Generator.

Reply

Join Date: Feb 2008
Posts: 3
Reputation: hollyadams is an unknown quantity at this point 
Solved Threads: 0
hollyadams hollyadams is offline Offline
Newbie Poster

Problem with Access Random Record Generator.

 
0
  #1
Jun 28th, 2008
Hi I am try to use some code that I found on Daniweb forum to display random records from an Access database on a page. The database has a table called tFact, and two columns called ID, and vFact. I can not get the code to work, and I was hoping someone would help me out? Here is the code:

  1. <script runat=server>
  2.  
  3. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  4. RandomFactNumber()
  5. RandomFact()
  6.  
  7. End Sub
  8.  
  9. Sub RandomFactNumber()
  10.  
  11.  
  12. strSQL = "SELECT ID FROM tFact"
  13.  
  14. Dim objDataReader as OledbDataReader
  15.  
  16.  
  17. DBconnect()
  18.  
  19. objConnection.Open()
  20. objDataReader = objCommand.ExecuteReader()
  21.  
  22.  
  23. Dim iRecordNumber = 0
  24. do while objDataReader.Read()=True
  25. iRecordNumber += 1
  26. loop
  27.  
  28. objDataReader.Close()
  29. objConnection.Close()
  30.  
  31.  
  32. Randomize()
  33. do
  34. iRandomFact = (Int(RND() * iRecordNumber))
  35. loop until iRandomFact <> 0
  36.  
  37.  
  38. End Sub
  39.  
  40.  
  41.  
  42.  
  43. Sub RandomFact()
  44.  
  45. strSQL = "SELECT ID, vFact FROM tFact as @ID, @vFact"
  46.  
  47. Dim objDataReader as OledbDataReader
  48.  
  49.  
  50. DBconnect()
  51.  
  52. objConnection.Open()
  53. objDataReader = objCommand.ExecuteReader()
  54.  
  55. Dim i = 0
  56.  
  57.  
  58. do while i<>iRandomFact
  59. objDataReader.Read()
  60. i += 1
  61. loop
  62.  
  63.  
  64.  
  65.  
  66.  
  67. Label1.Text = objDataReader("@ID")
  68. Label2.Text = objDataReader("@vFact")
  69.  
  70.  
  71. objDataReader.Close()
  72. objConnection.Close()
  73.  
  74. End Sub
  75.  
  76.  
  77.  
  78.  
  79. Sub DBconnect()
  80.  
  81. objConnection = New OledbConnection(strConnection)
  82. objCommand = New OledbCommand(strSQL, objConnection)
  83.  
  84. End Sub
  85.  
  86.  
  87.  
  88. Public strDBLocation = DB_Path()
  89. Public strConnection as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBLocation
  90. Public objConnection
  91. Public objCommand
  92. Public strSQL as string
  93. Public strRatingimg as Integer
  94. Public iRandomFact as integer
  95.  
  96.  
  97. Function DB_Path()
  98.  
  99. If InStr(Context.Request.ServerVariables("PATH_TRANSLATED"), "tFact") Then
  100. DB_Path = System.Web.HttpContext.Current.Server.MapPath("App_Data\QEI.mdb")
  101. Else
  102. DB_Path = System.Web.HttpContext.Current.Server.MapPath("App_Data\QEI.mdb")
  103. End If
  104.  
  105. End Function
  106.  
  107. </script>


Any ideas what I'm doing wrong? Let me know if you need anymore information, I'm pretty new to this stuff. Thank you.

Holly
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 48
Reputation: Ken Sharpe is an unknown quantity at this point 
Solved Threads: 2
Ken Sharpe's Avatar
Ken Sharpe Ken Sharpe is offline Offline
Light Poster

Re: Problem with Access Random Record Generator.

 
0
  #2
Jul 2nd, 2008
That code is insane. Horribly inefficient -- here's what you need to do instead.

Execute a query that gets a COUNT of all the records.

Get a random number between 1 and that count. We'll call that X.

Select one record at index X-- NOT with ID X, that won't work, INDEX X.

The one record you have will be your random fact.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the ASP.NET Forum


Views: 1126 | Replies: 1
Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC