Thursday, 29 January 2015

Get Distinct Value From DataTable Using C#.Net and VB.Net

1/29/2015 - By Pranav Singh 0

This article will show you how you can retrieve distinct value from a datatable using c#.net. in this I will show you to get the distinct values of the datatable column.


Some listbox articles are as follows: Bind and Retrieve ListBox Selected Value in MVC Using C#.Net, Stylish RadioButton, DropdownList, ListBox and TextBox Control In Asp.Net Using Css, Move ListBox Items From one ListBox To another ListBox Using C#.Net In Asp.Net, How to add/move multiselected items from one listbox to another listbox in C#.Net and VB.Net | C# multiple selection listbox move Using C#.Net, Transfer Listbox Items to Another Listbox Using C#.Net and VB.Net | How to Move List Box Items to another List Box in C#

So for this article first we will create a new application and retrieve data from database in a datatable using c#.net and vb.net.

But first please check the table data.


C#.net
SqlConnection con = new SqlConnection(System.Configuration.
ConfigurationManager.ConnectionStrings["con"].ToString());
            try
            {
                DataTable objdt = new DataTable();
                string query = "select * from UserDetail;";
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                con.Open();
                da.Fill(objdt);
                con.Close();
                if (objdt.Rows.Count > 0)
                {
                    DataView objdbview = new DataView(objdt);
                    DataTable objdistinctitem = objdbview.ToTable(true, "UserType");
                }
            }
            catch
            {
                con.Close();
            }
        }


Here in above code please check the highlighted code.  This is used for retrieving the distinct value.

                    DataView objdbview = new DataView(objdt);
                    DataTable objdistinctitem = objdbview.ToTable(true, "UserType");

Now here is the output of the code.



Now we will retrieve distinct value on the bases of multiple column. Please check the modified code. First please check the sql table data.



Now check the code

DataView objdbview = new DataView(objdt);
DataTable objdistinctitem = objdbview.ToTable(true, "UserType", "Name");

Now check the output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top