A unique method that can return the value of a column in a datarow and automatically manage the possibility that the value of the column is null

advertisements

Can anyone suggest a single method which can return the value of a column in a datarow and automatically handle the possibility of the column value to be null. Essentially I was trying to come up with a generic solution taking advantage of the DataRow extension methods to handle DBNull values. My solution till now has been:

public static Nullable<T> SafeRead<T>(DataRow row, string fieldName) where T : struct
{
    if (row.HasColumn(fieldName))
    {
        return row.Field<Nullable<T>>(fieldName) ?? default(Nullable<T>);
    }
    else
        return default(Nullable<T>);
}

public static T SafeRead<T>(DataRow row, string fieldName) where T : class
{
    if (row.HasColumn(fieldName))
    {
        return row.Field<T>(fieldName) ?? default(T);
    }
    else
        return default(T);
}

But this obviously complains about method ambiguity since C# does not allow method overloading based on parameter contraints


The following will give you the value back from the datarow, or the default for the type if the value is DBNull.Value. If the field is not defined it will throw an ArgumentException.

using System;
using System.Data;

public static class DataAccess
{
    public static T GetValueOrDefault<T>(DataRow row, string fieldName)
    {
        if (!row.Table.Columns.Contains(fieldName))
        {
            throw new ArgumentException(
                string.Format("The given DataRow does not contain a field with the name \"{0}\".", fieldName));
        }
        else if (row[fieldName].Equals(DBNull.Value))
        {
            return default(T);
        }

        return row.Field<T>(fieldName);
    }
}

And here are a few simple tests:

[TestMethod]
public void GetValueOrDefault_ValueType_Test()
{
    const string FieldName = "Column";
    const int Expected = 5;

    DataTable dataTable = new DataTable();
    dataTable.Columns.Add(FieldName, typeof(int));

    DataRow row = dataTable.Rows.Add(Expected);

    int actual = DataAccess.GetValueOrDefault<int>(row, FieldName);
    Assert.AreEqual(Expected, actual);
}

[TestMethod]
public void GetValueOrDefault_ValueType_DBNull_Test()
{
    const string FieldName = "Column";

    DataTable dataTable = new DataTable();
    dataTable.Columns.Add(FieldName, typeof(int));

    DataRow row = dataTable.Rows.Add(DBNull.Value);

    int actual = DataAccess.GetValueOrDefault<int>(row, FieldName);
    Assert.AreEqual(default(int), actual);
}

[TestMethod]
public void GetValueOrDefault_ReferenceType_Test()
{
    const string FieldName = "Column";
    object expected = new object();

    DataTable dataTable = new DataTable();
    dataTable.Columns.Add(FieldName, typeof(object));

    DataRow row = dataTable.Rows.Add(expected);

    object actual = DataAccess.GetValueOrDefault<object>(row, FieldName);
    Assert.AreEqual(expected, actual);
}

[TestMethod]
public void GetValueOrDefault_ReferenceType_DBNull_Test()
{
    const string FieldName = "Column";

    DataTable dataTable = new DataTable();
    dataTable.Columns.Add(FieldName, typeof(object));

    DataRow row = dataTable.Rows.Add(DBNull.Value);

    object actual = DataAccess.GetValueOrDefault<object>(row, FieldName);
    Assert.AreEqual(default(object), actual);
}