Solved

how to run Distinct query on DataTable?

Posted on 2011-03-11
5
647 Views
Last Modified: 2012-06-27
Hi, I have created DataTable “dt1” programmatically and enter query results in that datatable “dt1”.
Below is the code:
string strquery = "SELECT Name, Phones FROM Directory";
SqlCommand cmd = new SqlCommand(strquery, connection);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt1 = new DataTable();
da.Fill(dt1);
---------------------------------------------------------------------
Now, I want to run Distinct query on one of the column “Phone” or Name and result should get store in another datatable.

How it can be done?

Regards,  
0
Comment
Question by:prdeshpande
  • 4
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 400 total points
ID: 35107660
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 400 total points
ID: 35107666
HOW TO: Implement a DataSet SELECT DISTINCT Helper Class
http://support.microsoft.com/kb/326176
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 400 total points
ID: 35107671
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 400 total points
ID: 35107673
0
 
LVL 11

Assisted Solution

by:Sudhakar Pulivarthi
Sudhakar Pulivarthi earned 100 total points
ID: 35108374
Hi

// Get the distinct values applied on the column
string[] columns = {"Phone"};
DataTable distinctTable = dataTable.DefaultView.ToTable(true, columns);
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

11 Experts available now in Live!

Get 1:1 Help Now