C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ADO.NET »

Shuffling Data Values : Make it simple with using newid()


Posted Date: 18 Oct 2008    Resource Type: Code Snippets    Category: ADO.NET
Author: Gaurav AroraMember Level: Diamond    
Rating: 1 out of 5Points: 10




1. Design Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="shuffleresults.aspx.cs" Inherits="shuffleresults" %>

<!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>How to Shuffle Result-Sets</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid ID="dgEmp" runat="server" AlternatingItemStyle-BackColor="AliceBlue">
<AlternatingItemStyle BackColor="AliceBlue" />
</asp:DataGrid>
</div>
<br />
<div>
<asp:Button ID="btnShuffle" runat="server" ToolTip="Click to Shuffle Resuts" Text="Shuffle-Results"
OnClick="btnShuffle_Click" />
<asp:Button ID="btnNormal" runat="server" ToolTip="Click to Check Normal Result"
Text="Normal Results" OnClick="btnNormal_Click" />
</div>
</form>
</body>
</html>

2. Code-Behind Page

/* This Example is a part of different
* examples shown in Book:
* C#2005 Beginners: A Step Ahead
* Written by: Gaurav Arora
* Reach at : MsDotNetHeaven.com */

#region Code Region
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class shuffleresults : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData("ID");
}

}
void BindData(string strFldName)
{
SqlConnection myCon = new SqlConnection("Server=(local);uid=sa;pwd=;database=hrnpayroll");
SqlCommand myCmd = new SqlCommand("Select Top 10 * from Employees order by " + strFldName, myCon);
myCon.Open();
SqlDataReader myDr = myCmd.ExecuteReader();
dgEmp.DataSource = myDr;
dgEmp.DataBind();

myDr.Close();
myCon.Close();
}

protected void btnShuffle_Click(object sender, EventArgs e)
{
//Here just pass the newid() function to shuffle the output and see the magic

BindData("newid()");
}
protected void btnNormal_Click(object sender, EventArgs e)
{
//reset the dataresult
BindData("id");
}
}
#endregion Code Region


In above whenever the query execute, every time a new rowid generates and shuffles the results.




Responses

Author: Venkat    20 Oct 2008Member Level: Silver   Points : 0
Hi,
Really good article.


Author: Gaurav Arora    20 Oct 2008Member Level: Diamond   Points : 1
Hi Venkat,

Thanks for your words. I will be please if you send me your queries.

Thanks & regardsm
Gaurav Aroa


Author: Gaurav Arora    20 Oct 2008Member Level: Diamond   Points : 1
Hi Venkat,

Thanks for your words. I will be please if you send me your queries.

Thanks & regardsm
Gaurav Aroa


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
SQL  .  ADO.Net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Linq in ASP.NET
Previous Resource: View records one by one from ds on click of buttons
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use