I was recently looking for a method that would randomize a list of links displayed on ASPFree.com. Thanks to Scott Watermasysk (TripleASP.NET founder) passed along a tip that would randomize data coming from MS SQL Server. If the SQL statement that is SELECT * FROM TABLE ORDER BY NewId() , the returning results are scrambled. This was a nice way to re-arrange a result set. This is example can be used either in ASP.NET or classic ASP. WebPage code RandomSQL.aspx <%@ Page Language="VB" Debug="true" %> <%@ Import Namespace="System.data" %> <%@ Import Namespace="System.data.sqlclient" %>
<script language=vb runat=server> Sub Page_Load(Sender as Object, E as EventArgs) Dim conn As SqlConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=databaseName") Dim cmd As SqlCommand = New SqlCommand("Select * From NewId Order by NewID()", conn) conn.Open() DataGrid1.DataSource = cmd.ExecuteReader DataGrid1.DataBind() conn.Close() End Sub </script>
<HTML> <HEAD><title>WebForm1</title></HEAD> <body> <form id="Form1" method="post" runat="server"> <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid> </form> </body> </HTML> | RandomSQL.asp
<% Dim conn, rs strconn = "Driver={SQL Server};Description=sqldemo;SERVER=127.0.0.1;UID=sa;PWD=;DATABASE=databaseName"
set conn = server.createobject("adodb.connection") conn.open strconn set rs = conn.execute("Select * from NewId Order by NewId()") %> <html> <body> <% Do While Not rs.EOF response.write rs(0) & "<br>" rs.movenext Loop
conn.close set conn = nothing set rs = nothing %> </body> <html> | Table DDL
CREATE TABLE [dbo].[NewId] ( [NewId] [int] NULL ) ON [PRIMARY] GO
Sample Data used
1,2,3,4,5 | | DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP Code Articles More By aspfree developerWorks - FREE Tools! | You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
| | | | Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance. FREE! Go There Now!
| | | | Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. FREE! Go There Now!
| | | | Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
| | | | Learn from the best! Find out how developers use Rational ClearCase to be more flexible, innovative and deliver higher quality code in the Rational ClearCase Power Users eKit. This complimentary eKit provides a collection of materials, like articles, whitepapers, and demos that can help you become a power user of Rational ClearCase. FREE! Go There Now!
| | | | XML has become a common way of storing business data as flat files and many data server vendors including IBM have provided ways to store this data within relational database systems. Increasingly collections of XML files are accessed like databases using an xQuery and other XML standard mechanisms. Businesses find the need to combine the traditional tabular structured data with XML formatted data. In this webcast, you’ll learn about IBM’s WebSphere Federation Server technology, which provides users with the ability to integrate these two data formats. FREE! Go There Now!
| | | | This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”. FREE! Go There Now!
| | | | This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
| | | | Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications. FREE! Go There Now!
| | | | WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |