Random Records

Last post 07-01-2008 2:15 PM by hollyquinn. 3 replies.

Sort Posts:

  • Random Records

    06-25-2008, 3:40 AM
    • Loading...
    • aligenius
    • Joined on 04-22-2008, 6:09 AM
    • Posts 92

    Hi! Please help me to find a solution that my records will appear on the datagrid that random-access accordingly to their ID Field.

    I m using Access Database.

    Thanks

    Filed under:
  • Re: Random Records

    06-25-2008, 3:52 AM
    • Loading...
    • ca8msm
    • Joined on 11-29-2005, 3:57 PM
    • http://aspnetlibrary.com
    • Posts 2,134

    How can they be random if you want it based on an ID field? If they were truly random they would be selected based on no criteria.

    Mark,
    Join me at the new IT Community of the 21st Century: http://lessthandot.com

    Please remember to click "Mark as Answer" on this post if it helped you
  • Re: Random Records

    06-26-2008, 6:27 AM
    Answer

    SELECT * FROM MyTable
    ORDER BY Rnd(-10000000*TimeValue(Now())*[ID])

    Replace * with the fields/columns you want to select, and MyTable with the name of your table and ID with the name of your ID Field.

     

    Regards Mike
    [MVP - ASP/ASP.NET]
  • Re: Random Records

    07-01-2008, 2:15 PM
    • Loading...
    • hollyquinn
    • Joined on 06-24-2008, 9:52 AM
    • Posts 18

    Hi, as I've been looking for information about how to display a random record from an Access database, I keep running across your SQL statement above as replies to different messages.  Would you mind to provide a little more information on how to utilize this?  I am trying to display a random record from an access database.  Right now there are 10 records.  Just two columns ID and vFact (which is a string).  This is the code I currently have:

     

    <%@ Page Language="VB" AutoEventWireup="true" CodeFile="randomfact.aspx.vb" Inherits="randomfact" %>

    <%@ Import Namespace="System.Data" %>

    <%@ Import Namespace="System.Data.OleDb" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitled Page</title>

    <script language="VB" runat="server">

     

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

     

     

    Dim connString As String

    connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + "C:\Database\QEI.mdb;"

     

     

    Dim objConnection As OleDbConnectionobjConnection = New OleDbConnection(connString)

    objConnection.Open()

     

     

    Dim strSQL As String = "SELECT vFact FROM tFact ORDER BY Rnd(-10000000*TimeValue(Now())*[ID])"

     

     

    Dim objCommand As OleDbCommandobjCommand = New OleDbCommand(strSQL, objConnection)

     

    Dim objDataReader As OleDbDataReader

    objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

     

    fact.DataSource = objDataReader

    fact.DataBind()

     

     

    objDataReader.Close()

    End Sub

    </script>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:DataGrid id="fact" runat="server" />

    <br />

    <br />

    </div>

    </form>

    </body>

    </html>

    Unfortunately, the SQL statement doesn't work.  I get the error message:  "No value given for one or more required parameters." on the code line:

    objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

    Any idea what I am doing wrong?  Thank you.

     

    Holly

    Holly Quinn
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter