• Categories

  • Archives

How to retrieve a random records in ASP.NET

Recently i needed to pull some random records from the database. One of the proposed solutions was something like this:

<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyName\MyDBfolder\DBfile.mdb" providername="System.Data.OleDb" selectcommand="SELECT TOP 1 *, Rnd([id]) AS Expr1 FROM Books ORDER BY Rnd([id]) DESC"></asp:sqldatasource>

This works in the query builder but not when you run the application.

The reason that this doesn’t work is because the rnd method will return the same values over and over again, unless the randomize method is called! But there is another solution that will return a different order without calling the randomize method. The rnd method will also return a different number if a diferent parameter is used. So instead of using the ID value as the parameter, we should use a value that is different every time (now()) and for every record ([Id])

SELECT
TOP 1 * FROM Books
ORDER BY Rnd(-10000000*TimeValue(Now())*[id])

MS SQL Server
The above example will work with MS Access. For Microsoft SQL Server you’ll use the NewId() function to return values of data type unique indentifier. These are similar to Windows GUIDs in that the current date/time is used to seed the random value generator. Again, you’ll use the ordinal position of the “random value” column to order the resultset.

SELECT
   TOP 1 CategoryID,
   CategoryName
FROM
   Categories
ORDER BY
   NewID()

Note: This is not going to be a light operation if you have many, many records (say, a few hundred thousand). However, if you have use a WHERE clause to restrict the number of records that you are creating GUIDs for, then SQL Server will apply that before creating the GUIDs. For instances where you wish to return just a single record (rather than, say 5 random records), it may be better to create a static table with sequentially numbered records, and use a random number generator to determine which record should be returned.

ASP.NET Best Practices for High Performance Applications

As i’m just starting to optimize my asp.net site, this article will help me building a simple checklist. Saved for personal reference 🙂

ASP.NET is much more powerful than classic ASP, however it is important to understand how to use that power to build highly efficient, reliable and robust applications. In this article, I tried to highlight the key tips you can use to maximize the performance of your ASP.NET pages. The list can be much longer, I am only emphasizing the most important ones.

  1. Plan and research before you develop
  2. String concatenation
  3. Avoid round trips to the server
  4. Save viewstate only when necessary
  5. Use session variables carefully
  6. Use Server.Transfer
  7. Use server controls when appropriate and avoid creating deeply nested controls
  8. Choose the data viewing control appropriate for your solution
  9. Optimize code and exception handling
  10. Use a DataReader for fast and efficient data binding
  11. Use paging efficiently
  12. Explicitly Dispose or Close all the resources
  13. Disable tracing and debugging
  14. Precompile pages and disable AutoEventWireup
  15. Use stored procedures and indexes

Here is the full ASP.NET Best Practices for High Performance Applications article.

Cool Free ASP.NET AJAX Rich Text Editor

Kannan Sundarajan has written Rich Text Editor control using ASP.NET AJAX and shared it under the MS-PL license on CodePlex. It has a very rich feature set and Kannan hopes to enhance it further in the future. Since it is a CodePlex project you can report issues and make feature requests.

Check out the live demo and see Kirti’s blog for the full run down and some great screen shots.

Highslide JS .NET

Highslide JS .NET is a .NET control to encapsulate the functionality of Torstein Honsi’s excellent Highslide JS library.

Highslide JS is an open source JavaScript software, offering a Web 2.0 approach to popup windows. It streamlines the use of thumbnail images and HTML popups on web pages. The library offers these features and advantages:

  • No plugins like Flash or Java required.
  • Popup blockers are no problem. The content expands within the active browser window.
  • Single click. After opening the image or HTML popup, the user can scroll further down or leave the page without closing it.
  • Compatibility and safe fallback. If the user has disabled JavaScript or the JavaScript fails in any way, the browser redirects directly to the image itself or to a fallback HTML page. This fallback is able to cope with most exceptions and incompatibilities.

Why ASP.NET AJAX UpdatePanels are dangerous

Using Web methods instead full post backs…

Web methods allow ASP.NET AJAX pages to directly execute a page’s static methods, using JSON (JavaScript Object Notation). JSON is basically a minimalistic version of SOAP, which is perfectly suited for light weight communication between client and server. For more information about how to implement web methods and JSON, take a look at Microsoft’s Exposing Web Services to Client Script in ASP.NET AJAX.

Full article HERE.

Are you making these 3 common ASP.NET AJAX mistakes?

Check out Dave Wards advice on Update Panels and Postabacks HERE

In this post, I’d like to point out a few of the problems I’ve seen developers running into and what you can keep in mind to avoid them:

  • Page events still fire during partial postbacks.
  • UpdatePanel events fire, even when not updating.
  • Control event handlers fire after Load events.