SIGN UP MEMBER LOGIN:    
ARTICLE

Populating DataView from DataReader

Posted by Rajul Aggarwal Articles | Visual C# July 08, 2009
This article will illustrate how to populate DataView from DataReader in C#. In many scenarios we may have the data in DataReader which is required to be bind to GridView or some other control. In this scenario we can populate a DataView from DataReader and then provide the populated DataView to our Control.
Reader Level:
Download Files:
 

Populating DataView from DataReader

Introduction:

This article will illustrate how to populate DataView from DataReader in C#. In many scenarios we may have the data in DataReader which is required to be bind to GridView or some other control. In this scenario we can populate a DataView from DataReader and then provide the populated DataView to our Control.

Code Snippet:

For ease I am creating a function that will take a DataReader (or IDataReader) and Table Name as parameter and returns a populated DataView.

private DataView PopulateDataView(IDataReader dataReader, string tableName)

    {

        DataTable dataReaderTable = new DataTable(tableName);

        try

        {

            for (int count = 0; count < dataReader.FieldCount; count++)

            {

                DataColumn tempCol = new DataColumn(dataReader.GetName(count), dataReader.GetFieldType(count));

                dataReaderTable.Columns.Add(tempCol);

            }

            while (dataReader.Read())

            {

                DataRow dr = dataReaderTable.NewRow();

                for (int i = 0; i < dataReader.FieldCount; i++)

                {

                    dr[i] = dataReader.GetValue(dataReader.GetOrdinal(dataReader.GetName(i)));

                }

                dataReaderTable.Rows.Add(dr);

            }

            return dataReaderTable.DefaultView;

        }

        catch

        {

            return null;

        }

    }

Explanation:

Please find the explanation of the code listed above as under.

1.       At the beginning of the method, we created an instance of temporary DataTable named 'dataReaderTable' by passing in the table name to it.

2.       We defined the columns of the 'dataReaderTable' by iterating through the DataReader for retrieving Column name and its DataType.

3.       We iterated through the DataReader and inserted the data from it into DataTable using a DataRow.

4.       To return DataView from the method, dataReaderTable.DefaultView is returned.

5.       Whole of the code block is inserted into a Try and a Catch block. In case any exception is generated then null is returned from the method.

Conclusion:

With this method we need not to populate the DataSet so as to provide it as a datasource to a binding control. We can also use the DataReader and fill DataView with it and use this Dataview as a source for a binding control.

Login to add your contents and source code to this article
share this article :
post comment
 

very nice code..

Posted by jcortiz1 May 15, 2011

thanx Rajul for your article on populating a DataView. I used it to fill a Crystal Report and be able to change reports in a report viewer in a snap. It saved me hours. thanx again. BR Per

Posted by Per Malmstedt Aug 13, 2009
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor