944,147 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 16006
  • ASP.NET RSS
Jun 30th, 2005
0

ASP .NET Display Random records from the database

Expand Post »
Here is the code I use in the World recipe random record. I've been looking all over the net on how to display random record using access db on ASP .NET, but found most for SQL server like NEWID(), doesn't work on Access. This could be easily done in ASP 3.0. So I do some research on it, and came with this code.

Note: This does not use code behind, it's inline. Written entirely in notepad. I have VS .NET but decide not to use it.


LIVE DEMO: http://www.myasp-net.com

ASP.NET Syntax (Toggle Plain Text)
  1. <%@ Page Language="VB" Debug="true" %>
  2. <%@ import Namespace="System.Data" %>
  3. <%@ import Namespace="System.Data.Oledb" %>
  4.  
  5. <script runat="server">
  6.  
  7. 'Pulls a Random number for selecting a random recipe
  8. Sub RandomRecipeNumber()
  9.  
  10. 'It connects to database
  11. strSQL = "SELECT ID FROM Recipes"
  12.  
  13. Dim objDataReader as OledbDataReader
  14.  
  15. 'Call Open database - connect to the database
  16. DBconnect()
  17.  
  18. objConnection.Open()
  19. objDataReader = objCommand.ExecuteReader()
  20.  
  21. 'Counts how many records are in the database
  22. Dim iRecordNumber = 0
  23. do while objDataReader.Read()=True
  24. iRecordNumber += 1
  25. loop
  26.  
  27. objDataReader.Close()
  28. objConnection.Close()
  29.  
  30. 'Here's where random number is generated
  31. Randomize()
  32. do
  33. iRandomRecipe = (Int(RND() * iRecordNumber))
  34. loop until iRandomRecipe <> 0
  35.  
  36.  
  37. End Sub
  38.  
  39.  
  40.  
  41. 'Pulls aand dsiplay random recipe records
  42. Sub RandomRecipe()
  43.  
  44. strSQL = "SELECT ID,CAT_ID,Category,Name,Author,Date,HITS,RATING,NO_RATES, (RATING/NO_RATES) AS Rates FROM Recipes"
  45.  
  46. Dim objDataReader as OledbDataReader
  47.  
  48. 'Call Open database - connect to the database
  49. DBconnect()
  50.  
  51. objConnection.Open()
  52. objDataReader = objCommand.ExecuteReader()
  53.  
  54. Dim i = 0
  55.  
  56. 'Go until a random position
  57. do while i<>iRandomRecipe
  58. objDataReader.Read()
  59. i += 1
  60. loop
  61.  
  62. Dim strRanRating as Double
  63.  
  64. 'Display recipe
  65. lblRating2.Text = "Rating:"
  66. lblRancategory.text = "Category:"
  67. lblranhitsdis.text = "Hits:"
  68. lblranhits.text = objDataReader("Hits")
  69. strRanRating = FormatNumber(objDataReader("Rates"), 1, -2, -2, -2)
  70. lblranrating.Text = "(" & strRanRating & ")"
  71. strRatingimg = FormatNumber(objDataReader("Rates"), 1, -2, -2, -2)
  72.  
  73. LinkRanName.NavigateUrl = "recipedetail.aspx?id=" & objDataReader("ID")
  74. LinkRanName.Text = objDataReader("Name")
  75. LinkRanName.ToolTip = "View" & " - " & objDataReader("Name") & " - " & "recipe"
  76. LinkRanCat.NavigateUrl = "category.aspx?catid=" & objDataReader("CAT_ID")
  77. LinkRanCat.Text = objDataReader("Category")
  78. LinkRanCat.ToolTip = "Go to" & " - " & objDataReader("Category") & " - " & "&category"
  79.  
  80. objDataReader.Close()
  81. objConnection.Close()
  82.  
  83. End Sub
  84.  
  85.  
  86.  
  87. 'Database connection string - Open database
  88. Sub DBconnect()
  89.  
  90. objConnection = New OledbConnection(strConnection)
  91. objCommand = New OledbCommand(strSQL, objConnection)
  92.  
  93. End Sub
  94.  
  95.  
  96. 'Declare public so it will accessible in all subs
  97. Public strDBLocation = DB_Path()
  98. Public strConnection as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBLocation
  99. Public objConnection
  100. Public objCommand
  101. Public strSQL as string
  102. Public strRatingimg as Integer
  103. Public iRandomRecipe as integer
  104.  
  105. 'Get the database server Map Path - This is where you change the database name and path
  106. Function DB_Path()
  107.  
  108. if instr(Context.Request.ServerVariables("PATH_TRANSLATED"),"Recipes") then
  109. DB_Path = System.Web.HttpContext.Current.Server.MapPath("MyRecipes.mdb")
  110. else
  111. DB_Path = System.Web.HttpContext.Current.Server.MapPath("/mydb/MyRecipes.mdb")
  112. end if
  113.  
  114. End Function
  115.  
  116. </script>
  117.  
On the page_load call the random subs

'Call Random Recipe
RandomRecipeNumber()
RandomRecipe()


LIVE DEMO: http://www.myasp-net.com

Have fun!
Last edited by Paladine; Sep 1st, 2005 at 2:21 pm. Reason: ** Added Code Blocks
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dexterz is offline Offline
86 posts
since Feb 2005
Jun 30th, 2005
0

Re: ASP .NET Display Random records from the database

Demo says service unavailable
Reputation Points: 10
Solved Threads: 0
Light Poster
jerMAn is offline Offline
28 posts
since Jun 2005
Jul 29th, 2005
0

Re: ASP .NET Display Random records from the database

Demo is working

Demo Link: http://www.myasp-net.com


Dexter
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dexterz is offline Offline
86 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: I Need Sample ASP.NET Code
Next Thread in ASP.NET Forum Timeline: URGENT! Loading Roles From ticket.UserData





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC