Wednesday, November 21, 2012

Sort table rows randomly in SQL Server

 

SELECT * FROM table
ORDER BY NEWID()


In the above example NEWID() creates a unique value of type uniqueidentifier for each row and sort the rows accordingly.


Below is the example for retrieving a single random record from a table


SELECT TOP 1 * FROM table
ORDER BY NEWID()


Happy coding…

Thursday, November 1, 2012

Retrieving top 10 records from Data Table using LINQ in ASP.NET

The below code will retrieve top 10 records from the data table.

DataTable1.AsEnumerable().Take(10);


Note: Add using System.Linq on the top.


If intellesense is not showing up, add reference to System.Core


We can copy these records to another data table as below


DataTable1.AsEnumerable().Take(10).CopyToDataTable();


Happy coding…

How to truncate text while data binding

How to truncate text in a label control to allow maximum number of characters. I would like to avoid using code behind because the label is in a repeater.

<%
   1: # Eval("EventDate").ToString().Substring(startIndex, Length of string)
%>


Happy coding…