Rows.Count, Column.Count and IEnumerator using ADO.NET with C# and Database Server

Rows.Count, Column.Count and IEnumerator using ADO.NET with C# and Database Server


Begin your application by launching VS.NET and creating a new project using File - > Project and choose C# Windows Application template as shown in figure below:


Add a new item application configuration file as shown below from Project -> Add New Item


The code for the application configuration file:


<?xml version="1.0" encoding="utf-8" ?>



    <add name="constr" connectionString="initial catalog=puran; data source=MCN002; integrated security=sspi"/>




Note: In the above code I have made a connection using windows authentication.


In the form design view drop a DataGrid and 4 buttons. Name the first button as Fill, second as Rows Count, third as Columns Count and fourth as View Records. Add five text boxes also, in which we will display the records using IEnumertor.


Add the necessary reference file from the solution explorer – Reference as below:


The code for the application is as:


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Configuration;

using System.Data.SqlClient;

using System.Windows.Forms;

using System.Collections;


namespace rows_columns_count


    public partial class Form1 : Form


        public Form1()




        SqlDataAdapter da;

        DataSet ds;

        IEnumerator i;


        private void button1_Click(object sender, EventArgs e)


            da = new SqlDataAdapter("select * from testpuran", ConfigurationManager.ConnectionStrings["constr"].ConnectionString);

            ds = new DataSet();


            dataGrid1.DataSource = ds;

            i = ds.Tables[0].Rows.GetEnumerator();



        private void button2_Click(object sender, EventArgs e)


            int rows = ds.Tables[0].Rows.Count;

            MessageBox.Show("No. of Rows are : " + rows.ToString());



        private void button3_Click(object sender, EventArgs e)


            int cols = ds.Tables[0].Columns.Count;



            MessageBox.Show("No. of columns are : " + cols.ToString());



        private void button4_Click(object sender, EventArgs e)



            textBox1.Text = ((DataRow)i.Current)[0].ToString();

            textBox2.Text = ((DataRow)i.Current)[1].ToString();

            textBox3.Text = ((DataRow)i.Current)[2].ToString();

            textBox4.Text = ((DataRow)i.Current)[3].ToString();

            textBox5.Text = ((DataRow)i.Current)[4].ToString();

            textBox6.Text = ((DataRow)i.Current)[5].ToString();

            textBox7.Text = ((DataRow)i.Current)[6].ToString();





Output of the above program





Code Explanation:


In the above code the on click of Fill button DataGrid is used to display table information.


The code of RowsCount button shows the number of rows in the table.


The DataRowCollection.Count get the total number of System.Data.DataRow objects in the collection.


The code of ColumnsCount button shows the number of columns in the table.


The InternalDataCollectionBase.Count gets the total number of elements in the collection.


The code of ViewRecords button shows the table row information of each field in text boxes.


The IEnumerator is used to fetch record one by one in the textboxes. using System.Collections namespace is used to implement IEumerator interface.


The IEnumerable interface contains an abstract member function called GetEnumerator() and return an interface IEnumerator on any success call. This IEnumerator interface will allow us to iterate through any custom collection.


The IEnumerator provides two abstract methods and a property to pull a particular element in a collection. And they are Reset(), MoveNext() and Current.


Note: In the above article you have to change the database server, connection string and query statements accordingly.




Your feedback and constructive contributions are welcome. Please feel free to contact me for feedback or comments you may have about this article.

Up Next
    Ebook Download
    View all
    View all