DataTable For Loop to read rows C# VB.Net

In my previous article i have explained “DataTable Foreach Loop to Read Rows Data“. Though Foreach loop is the best choice to read DataTable rows but here i am showing one can read DataTable records using For Loop also. To do this example first need to create a DataTable, Populate DataTable by some rows then Loop through DataTable to retrieve all data.

Creating 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.

DataTable For Loop Code Syntax:
C# Code:

            string sColValues = "";
            for (int i = 0; i < oTable.Rows.Count; i++)
            {
                // Loop through DataTable Columns Collection
                for (int j = 0; j < oTable.Columns.Count; j++)
                    sColValues += oTable.Rows[i][j].ToString() + " --> ";
                Response.Write(sColValues + "</br>");
                sColValues = "";
            }

VB.Net Code:

        Dim sColValues As String = ""
        For i As Integer = 0 To oTable.Rows.Count - 1
            'Loop through DataTable Columns Collection
            For j As Integer = 0 To oTable.Columns.Count - 1
                sColValues += oTable.Rows(i)(j).ToString() + " --> "
            Next
            Response.Write(sColValues + "</br>")
            sColValues = ""
        Next

Since we know the number of DataTable Columns. We can ommit inner loop like:
C# Code:

            for(int i=0;i<oTable.Rows.Count;i++)
            {
                //Since we know the number of column of the DataTable
                Response.Write(oTable.Rows[i][0].ToString() + " --> " + oTable.Rows[i][1].ToString() + " --> " + oTable.Rows[i][2].ToString() + "</br>");
            }

VB.Net Code:

        For i As Integer = 0 To oTable.Rows.Count - 1
            'Since we know the number of column of the DataTable
            Response.Write(oTable.Rows(i)(0).ToString() + " --> " + oTable.Rows(i)(1).ToString() + " --> " + oTable.Rows(i)(2).ToString() + "</br>")
        Next

Since we know the DataTable Column Names. So that we can write in the following way:
C# Code:

            for(int i=0;i<oTable.Rows.Count;i++)
            {
                //Since we know the name of the columns of DataTable
                Response.Write(oTable.Rows[i]["ID"].ToString() + " --> " + oTable.Rows[i]["Title"].ToString() + " --> " + oTable.Rows[i]["Published"].ToString() + "</br>");
            }

VB.Net Code:

        For i As Integer = 0 To oTable.Rows.Count - 1
            'Since we know the number of column of the DataTable
            Response.Write(oTable.Rows(i)("ID").ToString() + " --> " + oTable.Rows(i)("Title").ToString() + " --> " + oTable.Rows(i)("Published").ToString() + "</br>")
        Next

Output Looks Like:
DataTable_Foreach_Loop

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net

Leave a Reply

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

     

*