DataTable Foreach Loop to Read Records C# VB.Net Asp.Net

DataTable contains a set of records and a DataRow contains an individual row of a DataTable. So to iterate or loop all DataTable Records we need to use DataRow object. In this lesson i will show how one can use Foreach loop to retrieve records from a DataTable.

First Create a DataTable:
C# Code:

            DataTable oTable = new DataTable("Article");

            //Add DataTable column dynamically/run time/on the fly.
            oTable.Columns.Add(new DataColumn("ID", typeof(System.Int64)));
            oTable.Columns.Add(new DataColumn("Title", typeof(System.String)));
            oTable.Columns.Add(new DataColumn("Published", typeof(System.DateTime)));

            //Add DataTable rows dynamically/run time/on the fly.
            oTable.Rows.Add(1001, "DataTable Engineering", DateTime.Now);
            oTable.Rows.Add(1002, "Event Calendar", DateTime.Now);
            oTable.Rows.Add(1003, "Master Detail Data", DateTime.Now);

Note: Don’t forget to add “using System.Data;” namespace.

VB.Net Code:

            Dim oTable As DataTable = New DataTable("Article")

            'Add DataTable column dynamically/run time/on the fly.
            oTable.Columns.Add(New DataColumn("ID", System.Type.GetType("System.Int64")))
            oTable.Columns.Add(New DataColumn("Title", System.Type.GetType("System.String")))
            oTable.Columns.Add(New DataColumn("Published", System.Type.GetType("System.DateTime")))

            'Add DataTable rows dynamically/run time/on the fly.
            oTable.Rows.Add(1001, "DataTable Engineering", DateTime.Now)
            oTable.Rows.Add(1002, "Event Calendar", DateTime.Now)
            oTable.Rows.Add(1003, "Master Detail Data", DateTime.Now)

Note: Don’t forget to add “Imports System.Data” namespace.

Now time to Looping over DataTable using Foreach Loop:
C# Code:

            //Reading DataTable Rows Column Value using Column Index Number
            foreach (DataRow oRecord in oTable.Rows)
                Response.Write(oRecord[0].ToString() + " -> " + oRecord[1].ToString() + " -> " + oRecord[2].ToString()+"</br>");

VB.Net Code:

            'Reading DataTable Rows Column Value using Column Index Number
            For Each oRecord As Object In oTable.Rows
                Response.Write(oRecord(0).ToString() + " -> " + oRecord(1).ToString() + " -> " + oRecord(2).ToString() + "</br>")
            Next

Note: You can use DataRow instead of Object in Foreach loop.

You can also loop through DataTbale Rows/Records by using Column Name:
C# Code:

            foreach (DataRow oRecord in oTable.Rows)
                Response.Write(oRecord["ID"].ToString() + " -> " + oRecord["Title"].ToString() + " -> " + oRecord["Published"].ToString() + "</br>");

VB.Net Code:

            For Each oRecord As Object In oTable.Rows
                Response.Write(oRecord("ID").ToString() + " -> " + oRecord("Title").ToString() + " -> " + oRecord("Published").ToString() + "</br>")
            Next

Note: You can use DataRow instead of Object in Foreach loop.

Find a sample output from below:
DataTable_Foreach_Loop

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code