1

I'm trying to use a DataTable to perform calculations on rows of data using the Expression property on DataColumns. msdn

I found that if there is an exception while applying the expression to the rows, the default value will be used. That's exactly what I want for the row that has the problem. The issue is that each subsequent row will use the default value instead of evaluating the expression.

In this example start with a base table consisting of two columns and 10 rows. The first column is an auto-incremented column with a range of -5 to 5. The second column is always 3.

I add a third column with an expression that divides the "3" column by the first column. That will cause a div0 exception for one row.

For that one row I want it to use the default value I supplied. It does, but also applies the default value to the 4 remaining rows.

Output:

-5 3 -0.6
-4 3 -0.75
-3 3 -1
-2 3 -1.5
-1 3 -3
0 3 -33   //Expected
1 3 -33   //I want it to continue evaluating the expression, not default 
2 3 -33
3 3 -33
4 3 -33

How can I add handling so that if an individual row has problems it will use the default value while continuing to use the expression for subsequent rows?

Please note that I cant simple use an IIF as described here to check for problems like this because the Expressions used are dynamic and supplied from a user as a way to customize data.

Complete code to reproduce this:

using System;
using System.Data;

namespace DataSetExpressionTest
{
    public class SO
    {
        private static DataTable table = null;

        private static void Main(string[] args)
        {
            table = CreateBaseTable(); 

            //If there is a problem only in some rows of the table, the rows before the error are calculated using the expression. All rows afrer use the default value
            DataColumn onlyOneException = new DataColumn()
            {
                ColumnName = "One-Ex",
                Expression = "third / index",
                DefaultValue = -33
            };

            AddCalculationToTable(onlyOneException);

            PrintDataTable(table);
            Console.ReadLine();
        }

        private static void AddCalculationToTable(DataColumn calculationColumn)
        {
            //You can wrap individual stats in try/catch blocks. If there is an exception in a calc, it will use the default value specified
            try
            {
                table.Columns.Add(calculationColumn);
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught a calculation exception while adding column {0}. Will use default value instead!!", calculationColumn.ColumnName);
            }
        }

        private static DataTable CreateBaseTable()
        {
            DataTable table = new DataTable();

            // Create 3s column.
            DataColumn threeColumn = new DataColumn
            {
                DataType = Type.GetType("System.Decimal"),
                ColumnName = "third",
                DefaultValue = 3
            };

            //Create icrementing index column
            DataColumn indexColumn = new DataColumn
            {
                DataType = Type.GetType("System.Decimal"),
                ColumnName = "index",
                AutoIncrement = true,
                AutoIncrementSeed = -5
            };

            // Add columns to DataTable.
            table.Columns.Add(indexColumn);
            table.Columns.Add(threeColumn);

            for (var i = 0; i < 10; i++)
            {
                table.Rows.Add(table.NewRow());
            }
            return table;
        }

        public static void PrintDataTable(DataTable table)
        {
            foreach (DataColumn column in table.Columns)
            {
                Console.Write(column.ColumnName + " ");
            }

            Console.WriteLine();

            foreach (DataRow dataRow in table.Rows)
            {
                foreach (var item in dataRow.ItemArray)
                {
                    Console.Write(item + " ");
                }
                Console.WriteLine();
            }
        }
    }
}
Community
  • 1
  • 1
Chris Dibble
  • 388
  • 1
  • 5
  • 16

0 Answers0