ASP .NET Display Random records from the database

Please support our ASP.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Feb 2005
Posts: 86
Reputation: dexterz is an unknown quantity at this point 
Solved Threads: 3
dexterz dexterz is offline Offline
Junior Poster in Training

ASP .NET Display Random records from the database

 
0
  #1
Jun 30th, 2005
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

  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
Dexter Zaf
Ex-designz.net
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 28
Reputation: jerMAn is an unknown quantity at this point 
Solved Threads: 0
jerMAn jerMAn is offline Offline
Light Poster

Re: ASP .NET Display Random records from the database

 
0
  #2
Jun 30th, 2005
Demo says service unavailable
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 86
Reputation: dexterz is an unknown quantity at this point 
Solved Threads: 3
dexterz dexterz is offline Offline
Junior Poster in Training

Re: ASP .NET Display Random records from the database

 
0
  #3
Jul 29th, 2005
Demo is working

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


Dexter
Dexter Zaf
Ex-designz.net
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