Hi, I am trying to fill a DataGrid with random records from my database. It thought I had found a way to randomly select records using SQL. So I built a stored procedure in Access and sure enough, it returns a random set of records and works exactly how I need it to. But...
Whe I tried to call this from my ASP.Net page, it kept returning the same set of records. So I copied and pasted the SQL into the actual page(code below) but it made no difference. Could anyone tell me why this wouldn't work?
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDB" %>
<script runat="server">
Public objConn as new OleDbConnection (System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"))
Sub GetTeam(s as Object, E as EventArgs)
Dim objCmd As New OleDbCommand("SELECT TOP 4 PlayerID FROM tblPlayers WHERE PositionID='defender' ORDER BY rnd(isnull(PlayerID)*0+1);", objConn)
Try
objConn.Open()
dgDef.DataSource = objCmd.ExecuteReader
dgDef.DataBind()
objConn.Close()
Catch ex as OleDbException
throw ex
End try
End Sub
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {font-weight: bold}
-->
</style>
</head>
<body>
<form runat="server">
<p>
<asp:Button ID="btn33" runat="server" OnClick="GetTeam" Text="Get Defenders" />
</p>
<table width="100%" border="0" cellspacing="10" cellpadding="0">
<tr>
<td></td>
Defenders
<aspataGrid runat="server" ID="dgDef" EnableViewState="false" />
</tr>
</table>
<p> </p>
</form>
</body>
</html>
I have also tried:
SELECT TOP 4 PlayerID FROM tblPlayers WHERE PositionID='defender' ORDER BY newid()
But this doesn't work in Access or from my ASP.Net page and returns the error:
Undefined function 'newid' in expression
Any help would be greatly appreciated. I thought I had the first method working a few days ago. Are there any other factors that I could be overlooking? Like I said, the first option works fine when called within Access.
Many thanks
Shaun
Whe I tried to call this from my ASP.Net page, it kept returning the same set of records. So I copied and pasted the SQL into the actual page(code below) but it made no difference. Could anyone tell me why this wouldn't work?
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDB" %>
<script runat="server">
Public objConn as new OleDbConnection (System.Configuration.ConfigurationSettings.AppSettings("ConnectionString"))
Sub GetTeam(s as Object, E as EventArgs)
Dim objCmd As New OleDbCommand("SELECT TOP 4 PlayerID FROM tblPlayers WHERE PositionID='defender' ORDER BY rnd(isnull(PlayerID)*0+1);", objConn)
Try
objConn.Open()
dgDef.DataSource = objCmd.ExecuteReader
dgDef.DataBind()
objConn.Close()
Catch ex as OleDbException
throw ex
End try
End Sub
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.style1 {font-weight: bold}
-->
</style>
</head>
<body>
<form runat="server">
<p>
<asp:Button ID="btn33" runat="server" OnClick="GetTeam" Text="Get Defenders" />
</p>
<table width="100%" border="0" cellspacing="10" cellpadding="0">
<tr>
<td></td>
Defenders
<aspataGrid runat="server" ID="dgDef" EnableViewState="false" />
</tr>
</table>
<p> </p>
</form>
</body>
</html>
I have also tried:
SELECT TOP 4 PlayerID FROM tblPlayers WHERE PositionID='defender' ORDER BY newid()
But this doesn't work in Access or from my ASP.Net page and returns the error:
Undefined function 'newid' in expression
Any help would be greatly appreciated. I thought I had the first method working a few days ago. Are there any other factors that I could be overlooking? Like I said, the first option works fine when called within Access.
Many thanks
Shaun