Dealing with null values

6 posts, 0 answers
  1. Adrian
    Adrian avatar
    11 posts
    Member since:
    Apr 2012

    Posted 28 Nov 2012 Link to this post

    Hello there, I am assigning my axis in the following manner:
    ((Telerik.Windows.Controls.ChartView.ScatterPointSeries)xRadChartView_Cartesian.Series[0]).XValueBinding =
    new Telerik.Windows.Controls.ChartView.GenericDataPointBinding<System.Data.DataRow, Int32?>()
    {
    ValueSelector = row => (Int32?)row["ColumnHeader"]
    };

    This is so that a chart can be created with an axis that could contain null values.  However, when those null values are present, I instead receive an exception at the code above, but only at the point when I am setting the chart data source.  The exception is an InvalidCastException and says that the 'Specified cast is not valid.'.  This only happens if the null values are present.  If they are normal ints then it all works as expected.

    Is there an extra step I need to take to allow the chart to deal with these null values?  Either by displaying them as zeroes or just missing them out entirely, leaving just gaps?  The latter would be much more preferable.

    Thanks in advance.
  2. Missing user
    Missing user avatar

    Posted 30 Nov 2012 Link to this post

    Hello Adrian,

    Telerik's RadChartView can handle null values without any additional setup. From what I was able to see the InvalidCastException occurs when casting a DataRow's value to a nullable int.
    (Int32?)row["ColumnHeader"]
    As a suggestion, make sure the underlying type is indeed System.Nullable<Int32> and not some other database-specific type used by ADO.NET or that you convert your data to a nullable Int32 manually through a separate method.

    Best regards,
    Ivan N.
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Adrian
    Adrian avatar
    11 posts
    Member since:
    Apr 2012

    Posted 30 Nov 2012 Link to this post

    I probably should have posted this as well, I have checks like this:

    else if (myColumn.DataType == typeof(Int32?)
    {

    Code like the above surrounds code like what I posted in my first post.

    Does this constitute the appropriate checking that you mention?

  5. Missing user
    Missing user avatar

    Posted 05 Dec 2012 Link to this post

    Hi Adrian,

    I was thinking more along the lines of placing a breakpoint and examining the data at run-time by storing the result of the row["ColumnHeader"].GetType() method.

    Anyway, checking if a DataColumn's data type is Int32? should always fail, because ADO.NET doesn't support nullable types. Instead, it returns a DBNull object for database null values and a normal Int32 for non-null int values. This could be one of the possible reasons why null values in your database cannot be cast to a nullable int - there simply isn't a built-in type conversion between DBNull and System.Nullable Try replacing
    row => (Int32?)row["ColumnHeader"]
    with
    row =>
    {
        return row["ColumnHeader"] is DBNull ? null : (int?)row["ColumnHeader"];
    };

    I think this should solve your problem. Let me know how things have worked out.

    All the best,
    Ivan N.
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Adrian
    Adrian avatar
    11 posts
    Member since:
    Apr 2012

    Posted 05 Dec 2012 Link to this post

    Many thanks, your code seems to work.  I want to ask though, (I'm fairly new to this ADO.NET, sorry), if it doesn't support nullable datatypes, why does the check for a nullable int32 get satisfied?

    As in this:
    else if (((Telerik.Windows.Controls.GridViewDataColumn)horizontalAxisComboBox.SelectedItem).DataType == typeof(Int32?))

    Surely it should always go for the check for a normal Int32 rather than this then as if it doesn't support nulls the column datatype would never be set to a nullable Int32?




  7. Nick
    Admin
    Nick avatar
    593 posts

    Posted 05 Dec 2012 Link to this post

    Hi Adrian,

    Let me try to explain how the GridViewDataColumn works. When you databind the column to certain data, if there is no DataType explicitly set, it gets the type of the data you are binding to, in your case nullable int. Now, since the GridView logic relies on LINQ to execute actions like sorting, filtering, grouping, etc. it is required that the Columns have a certain DataType, which is assigned as described. 

    Hope this makes sense! Let me know if you need anything else. 

    Kind regards,
    Nik
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
UI for WPF is Visual Studio 2017 Ready