This is a migrated thread and some comments may be shown as answers.

Dealing with null values

5 Answers 132 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Adrian
Top achievements
Rank 1
Adrian asked on 28 Nov 2012, 12:50 PM
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.

5 Answers, 1 is accepted

Sort by
0
Missing User
answered on 30 Nov 2012, 04:50 PM
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.

0
Adrian
Top achievements
Rank 1
answered on 30 Nov 2012, 05:08 PM

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?

0
Missing User
answered on 05 Dec 2012, 10:17 AM
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.

0
Adrian
Top achievements
Rank 1
answered on 05 Dec 2012, 11:10 AM
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?




0
Nick
Telerik team
answered on 05 Dec 2012, 01:17 PM
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.

Tags
ChartView
Asked by
Adrian
Top achievements
Rank 1
Answers by
Missing User
Adrian
Top achievements
Rank 1
Nick
Telerik team
Share this question
or