Tuesday, 14 November 2017

Where Clause in DataTable VB.Net and C#.Net

11/14/2017 - By Pranav Singh 0

This article will show you how you can apply where clause or filter in a datatable using c#.net and vb.net.

So for this article we will create a new console application to perform an example. So first we will have a datatable.

private static DataTable FillStudentData()
        {
            DataTable dtstudent = new DataTable();

            //Adding columns to table Emp
            DataColumn colEmpid = new DataColumn("RollNo", typeof(System.Int32));
            DataColumn colName = new DataColumn("Name", typeof(System.String));
            DataColumn colDept = new DataColumn("Address", typeof(System.String));

            //Adding columns to datatable
            dtstudent.Columns.AddRange(new DataColumn[] { colEmpid, colName, colDept });

            //Adding data
            dtstudent.Rows.Add(1, "John Smith Brown", "Address 1");
            dtstudent.Rows.Add(2, "Carry Brown", "Address 2");
            dtstudent.Rows.Add(3, "Candle Pencil", "Address 3");
            dtstudent.Rows.Add(4, "Graham Bell", "Address 4");
            dtstudent.Rows.Add(5, "Peter Kevin", "Address 5");

            return dtstudent;
        }

Now add the below code.

C#.NET:
DataTable dtstudent = new DataTable();
            dtstudent = FillStudentData();
            DataRow[] filterData1 = dtstudent.Select("RollNo=3");
            foreach (DataRow item in filterData1)
            {
                Console.WriteLine("Roll No : " + item.Field<int>("RollNo") + ", Name : " + item.Field<string>("Name") + ", Address : " + item.Field<string>("Address"));
            }

VB.NET:
Dim dtstudent As New DataTable()
dtstudent = FillStudentData()
Dim filterData1 As DataRow() = dtstudent.[Select]("RollNo=3")
For Each item As DataRow In filterData1
       Console.WriteLine("Roll No : " + item.Field(Of Integer)("RollNo") + ", Name : " + item.Field(Of String)("Name") + ", Address : " + item.Field(Of String)("Address"))
Next

In above code check the highlighted part of the code. In this I have used select and passed the value to check the record for roll no 3. So after execution we will get the below 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