DataTable While Loop to Read Records C# VB.Net

In one of my previous article i have explained “DataTable Foreach Loop to Read Rows Data“. Though Foreach loop is the best choice to retrieve DataTable rows but here i am showing one can read DataTable records using While 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 While Loop Code Syntax:
C# Code:

            int i = 0;
            while (i < oTable.Rows.Count)
            {
                int j = 0;
                string sData = "";
                while (j < oTable.Columns.Count)
                {
                    sData += oTable.Rows[i][j].ToString() + " --> ";
                    j++;
                }
                Response.Write(sData+"</br>");
                i++;
            }

VB.Net Code:

        Dim i As Integer = 0
        While i < oTable.Rows.Count
            Dim j As Integer = 0
            Dim sData As String = ""
            While j < oTable.Columns.Count
                sData += oTable.Rows(i)(j).ToString() + " --> "
                j = j + 1
            End While
            Response.Write(sData + "</br>")
            i = i + 1
        End While

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

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

VB.Net Code:

        i = 0
        While i < oTable.Rows.Count
            ' Since we know the number of columns in the DataTable
            Response.Write(oTable.Rows(i)(0).ToString() + " --> " + oTable.Rows(i)(1).ToString() + " --> " + oTable.Rows(i)(2).ToString() + "</br>")
            i = i + 1
        End While

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

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

VB.Net Code:

        i = 0
        While i < oTable.Rows.Count
            ' Since we know the name of the DataTable Columns
            Response.Write(oTable.Rows(i)("ID").ToString() + " --> " + oTable.Rows(i)("Title").ToString() + " --> " + oTable.Rows(i)("Published").ToString() + "</br>")
            i = i + 1
        End While

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 *

     

*