Displaying the Contents (and Row Versions) of an ADO.NET DataSet

Was asked for this code a few days ago after a quick demo I did, so I thought I'd throw it out here. Its a quick code snippet that displays the contents of a DataSet for debugging purposes. But first, here is a quick code snippet that grabs a DataSet full of 2 DataTables.

        private void GetData()
            string cnStr = @"server=Mine;database=northwind;integrated security=true;";
            SqlConnection cn = new SqlConnection(cnStr);
            DataSet ds = new DataSet("MyDataSet");
            string customerSql = "SELECT CustomerID, CompanyName, City, Country"
                                   + " FROM Customers ORDER BY CompanyName";
            SqlCommand customerCmd = new SqlCommand(customerSql);
            customerCmd.CommandType = CommandType.Text;
            customerCmd.Connection = cn;
            SqlDataAdapter da = new SqlDataAdapter(customerCmd);
            da.Fill(ds, "Customers");
            string orderSql = "SELECT OrderID, CustomerID, OrderDate "
                                 + " FROM Orders ORDER BY OrderDate DESC";
            SqlCommand orderCmd = new SqlCommand(orderSql);
            orderCmd.CommandType = CommandType.Text;
            orderCmd.Connection = cn;
            da.SelectCommand = orderCmd;
            da.Fill(ds, "Orders");
            DisplayDataSet(ds, "My Data");

And here is the code snippet that iterates through the DataSet to show the DataTables:

        private void DisplayDataSet(DataSet ds, string title)
            //--- Loop through the DataTables
            foreach (DataTable table in ds.Tables)
                Debug.WriteLine("*** DataTable: " + table.TableName + "***");
                //--- Loop through each DataTable's DataRows
                foreach (DataRow row in table.Rows)
                    //--- Display the original values, if there are any.
                    if (row.HasVersion(System.Data.DataRowVersion.Original))
                        Debug.Write("Original Row Values => ");
                        foreach(DataColumn column in table.Columns)
                            Debug.Write(column.ColumnName + " = " + 
                                 row[column, DataRowVersion.Original] + ", ");
;         //--- Display the current values, if there are any.
                    if (row.HasVersion(System.Data.DataRowVersion.Current))
                        Debug.Write("Current Row Values ==> ");
                        foreach(DataColumn column in table.Columns)
                            Debug.Write(column.ColumnName + " = " + 
                                 row[column, DataRowVersion.Current] + ", ");

The code is pretty straightforward for DisplayDataSet. I loop through each table in the DataSet, then through the rows in each table, and finally through the columns in each row. Before checking each column, however, I first check to make sure the row has an Original version and/or a Current version. This is nice for displaying the original and current versions of a column's value when you allow the user to edit multiple rows and send all of the changes to the server in a batch. If you don't check if the row has an original version first and the row happens to have been a new row, then it will throw an exception.

I often take this code and modify it for the debugging purpose of the moment. I've used a couple that throw them into hierarchical DataGrids and make everything nice and pretty. But sometimes I just want a quick and dirty method with a quick and drity code base like this. There are tools out there that you can download to do this, but if all you need is a quick method to display the data and its row versions, this will do nicely too.

Oh ... and the code above assumes the following using statements:

using System;
using System.Data;
using System.Diagnostics;
Tweet Post Share Update RSS

Hi, I'm John Papa. I author this blog, create courses for Pluralsight and am a Google Developer Expert and Microsoft Regional Director. I travel speaking at events and train technology thought leaders