Solved

Sorting Data in a Dataset in ASP.NET

Posted on 2007-11-28
4
5,728 Views
Last Modified: 2013-11-26
I have a custom class that takes LoginID, FirstName, Lastname as params and does the following.
1) Query Active Directory for any people that match any of the three values above.
2) Plug the search results into a dataset and return this dataset object.

In my ASPX page, I have a repeater control and three textfields to allow a user to enter a firstname, lastname, login ID that is passed to the custom class. When the user clicks the button, the code-behind code calls the custom class, performs the search and returns a dataset. I want to be able to sort this data, but the code I am using does not seem to work. Am I doing something wrong in the code?
'**PART OF CLASS THAT RETURNS DATASET POPULATED WITH DATA FROM AN ACTIVE DIRECTORY QUERY

Dim ds As New DataSet("ADSearchResults")

Dim table As New DataTable("tblSearchResults")

table.Columns.Add("LoginID", System.Type.GetType("System.String"))

table.Columns.Add("Fname", System.Type.GetType("System.String"))

table.Columns.Add("Lname", System.Type.GetType("System.String"))
 

Dim result As SearchResult

For Each result In searcher.FindAll()

    Dim dr As DataRow = table.NewRow()

    dr(0) = result.GetDirectoryEntry.Properties.Item("SAMAccountName").Value

    dr(1) = result.GetDirectoryEntry.Properties.Item("givenName").Value

    dr(2) = result.GetDirectoryEntry.Properties.Item("sn").Value

    table.Rows.Add(dr)

Next
 

table.DefaultView.Sort = "Lname, fname, loginID"

ds.Tables.Add(table)

return ds
 
 
 
 

'**ON PAGE LOAD IN THE CODE BEHIND FORM...

repGrid.DataSource = ds

repGrid.DataMember = "tblSearchResults"

repGrid.Visible = True

repGrid.DataBind()
 
 
 
 

'**REPEATER USED IN ASPX PAGE

		<asp:Repeater ID="repGrid" runat="server" Visible="False">

		<HeaderTemplate>

		 <table border="1">

		  <tr>

		   <th>Login ID</th>

		   <th>Fname</th>

		   <th>Lname</th>

		  </tr>

		 </HeaderTemplate>

		<ItemTemplate>

		 <tr>

		  <td><%# DataBinder.Eval(Container, "DataItem.LoginID") %></td>

		<td><%# DataBinder.Eval(Container, "DataItem.Fname") %></td>

		<td><%# DataBinder.Eval(Container, "DataItem.LName") %></td>		  

		 </tr>

		</ItemTemplate>		

		<FooterTemplate>

		</table>

		</FooterTemplate> 

		</asp:Repeater>

Open in new window

0
Comment
Question by:PiedmontHealth
  • 2
  • 2
4 Comments
 
LVL 37

Accepted Solution

by:
samtran0331 earned 200 total points
ID: 20368576
I think what's happening is that when this runs:

table.DefaultView.Sort = "Lname, fname, loginID"
ds.Tables.Add(table)
return ds


yes, you've sorted the defaultview of the table, but what gets added to the ds is still the raw (unsorted) table, not the defaultview.

you can either:
1. make the function return a dataview
or
2. don't sort the table until right before you bind it
0
 

Author Comment

by:PiedmontHealth
ID: 20368727
I see. I have since read about the difference in DataSet and DateView. Makes sense. I changed the custom class to return a dataview of the dataset and in my codebehind, I did this...

        Dim dv As DataView = [RESULTING DATAVIEW FROM THE OBJECT CALL]
        repGrid.DataSource = dv
        repGrid.DataMember = "tblSearchResults"
        repGrid.Visible = True
        dv.Sort = "lname, fname, loginID"
        repGrid.DataBind()
Works like a champ. Thanks! I am slowly but surely getting this .NET stuff. ;)
0
 

Author Closing Comment

by:PiedmontHealth
ID: 31411516
I love this site! I have thrown away my bailing pail!
0
 
LVL 37

Expert Comment

by:samtran0331
ID: 20368813
since your function returns a dv, you can skip the extra "dim dv"

'Dim dv As DataView = [RESULTING DATAVIEW FROM THE OBJECT CALL]
repGrid.DataSource = FunctionThatReturnsADataview

btw, I think I worked with a former co-worker (or guy you replaced)...let's call him "Bob J. The Funny Guy"
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 24
Linq Help 1 35
c# LinkButton OnClientClick 2 34
Show/Hide ASP.NET controls with JQuery best practices 3 10
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now