tags:

views:

314

answers:

0

Hi All,

I am trying to create some line charts from a dataset and getting an error (WPF+WPF Toolkit + C#):

Nullable object must have a value

Here is a code that I use to add some data points to the chart:


ObservableCollection points = new ObservableCollection();

foreach (DataRow dr in dc.Tables[0].Rows)
{
    points.Add(new VelChartPoint()
    {
        Label = dr[0].ToString(),
        Value = double.Parse(dr[1].ToString())
    });
}

Here is a class VelChartPoint


public class VelChartPoint : VelObject, INotifyPropertyChanged 
{
    public DateTime Date
    {
        get;
        set;
    }

    public string Label
    {
        get;
        set;
    }

    private double _Value;
    public double Value
    {
        get { return _Value; }
        set 
        { 
            _Value = value;

            var handler = PropertyChanged;
            if (null != handler)
            {
                handler.Invoke(this, new PropertyChangedEventArgs("Value"));
            }
        }
    }

    public string FieldName
    {
        get;
        set;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public VelChartPoint() { }

}

So the problem occures in this part of the code


points.Add(new VelChartPoint
    {
        Name = dc.Tables[0].Rows[0][0].ToString(),
        Value = double.Parse(dc.Tables[0].Rows[0][1].ToString())
    }
);

I've made some tests, here are some results i've found out. This part of code does'nt work for me:


string[] labels = new string[] { "label1", "label2", "label3" };

foreach (string label in labels)
{
    points.Add(new VelChartPoint
        {
            Name = label,
            Value = 500.0
        }
    );
}

But this one works fine:


points.Add(new VelChartPoint
    {
        Name = "LabelText",
        Value = double.Parse(dc.Tables[0].Rows[0][1].ToString())
    }
);

Stack trace

" at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)\r\n at System.Nullable1.get_Value()\r\n at System.Windows.Controls.DataVisualization.Charting.LineSeries.UpdateDataPoint(DataPoint dataPoint) in C:\\dd\\WPF_1\\src\\wpf\\src\\ControlsPack\\WPFToolkit\\DataVisualization\\Charting\\Series\\LineSeries.cs:line 247\r\n at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.UpdateDataPoints(IEnumerable1 dataPoints) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\Series\DataPointSeries.cs:line 1005\r\n at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.UpdateDataPoints(IEnumerable`1 dataPoints) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\Series\DataPointSeriesWithAxes.cs:line 376\r\n at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.OnSizeChanged(Object sender, SizeChangedEventArgs e) in C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\Series\DataPointSeries.cs:line 915\r\n at System.Windows.SizeChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)\r\n at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)\r\n at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)\r\n at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)\r\n at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)\r\n at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)\r\n at System.Windows.FrameworkElement.OnRenderSizeChanged(SizeChangedInfo sizeInfo)\r\n at System.Windows.ContextLayoutManager.fireSizeChangedEvents()\r\n at System.Windows.ContextLayoutManager.UpdateLayout()\r\n at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)\r\n at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()\r\n at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()\r\n at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)\r\n at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)\r\n at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)\r\n at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)"

Please, help me to solve this error.

PS

Also noticed that this part of code works fine for me:


foreach (DataRow dr in dc.Tables[0].Rows)
{                     
    points.Add(new VelChartPoint
        {
            Name = dr.Field(labelid),
            Value = (dr.Field(valueid) * 1.0)
        });

        MessageBox.Show("point added.");
}

So if I add a MessageBox window, it works. If I remove the delay - it throw the error exception.