GridView can't handle DBNull in dynamic data row

2 posts, 0 answers
  1. Datafyer
    Datafyer avatar
    66 posts
    Member since:
    Jun 2008

    Posted 25 Aug 2016 Link to this post

    I am dynamically loading a small amount of data from a Sql Server table using the dynamic data row concept in this example.
    Everything is working however if one of the values is DBNull then when the column is sorted the grid displays as blank. Toggling the sort back to off shows the data again.

    To solve this I am simply casting to null when the value is DBNull.

    var value = reader.GetValue(x);
    row[column] = value == DBNull.Value
        ? null
        : value;


    This sounds to me like a bug.

  2. Stefan
    Stefan avatar
    1073 posts

    Posted 29 Aug 2016 Link to this post

    Hello Patrick,

    The reported behavior is due to a LINQ exception caused by a type mismatch. Generally, when a DynamicObject is used, the data engine resolves the data type of a given property based on the data type of the relevant property of the first item. So, in this particular scenario, it tries to apply an object of type DBNull to a property of type String. Thus, an ArgumentException is raised and internally caught.

    On the other hand, applying a null value to it a string property is a valid operation, thus the control is working as expected. I am afraid that there is not much we can do for this scenario, so I suggest you sticking to the approach you've already adopted.

    All the best,
    Stefan X1
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top