locked
System.InvalidCastException

    Question

  • I'm having a bugger of a time getting to the bottom of this. I'm getting an exception AFTER the following completes:

    this.dspGrid.ItemsSource = AppData.JobDsps(this.selectedOrder);
    

    Where AppData.JobDsps is

            public static IEnumerable<Dsp> JobDsps(JobOrdTable job) {
                try {
                    DbJobList.Connection.Open();
    
                    return from d in job.Dsps
                           orderby d.DspActLoadTime descending
                           select d;
                } catch (Exception ex) {
                    CEAL.CEALException(string.Format("Exception building dsp list for jobid [{0}]", job.JobId), ex);
                    return null;
                } finally {
                    DbJobList.Connection.Close();
                }
            }
    

    The exception is actually not happening until sometime after JobDsps returns an EntitySet and WPF tries to update the DataGrid. Here's the stacktrace:

    ERROR [10] (C:\ace-co-devel\Aceco.NET\ScaleLoad (VS2010)\ScScaleLoad\CEAL.cs:110) - System.InvalidCastException: Specified cast is not valid.
       at System.Data.SqlClient.SqlBuffer.get_Int32()
       at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
       at Read_Dsp(ObjectMaterializer`1 )
       at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
       at System.Data.Linq.EntitySet`1.Load()
       at System.Data.Linq.EntitySet`1.get_Count()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
       at MS.Internal.Data.EnumerableCollectionView.LoadSnapshotCore(IEnumerable source)
       at MS.Internal.Data.EnumerableCollectionView..ctor(IEnumerable source)
       at MS.Internal.Data.ViewManager.GetViewRecord(Object collection, CollectionViewSource cvs, Type collectionViewType, Boolean createView)
       at MS.Internal.Data.DataBindEngine.GetViewRecord(Object collection, CollectionViewSource key, Type collectionViewType, Boolean createView)
       at System.Windows.Data.CollectionViewSource.GetDefaultCollectionView(Object source, Boolean createView)
       at System.Windows.Data.CollectionViewSource.GetDefaultCollectionView(Object source, DependencyObject d)
       at System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable value)
       at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at Specified cast is not valid.
       at System.Data.SqlClient.SqlBuffer.get_Int32()
       at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
       at Read_Dsp(ObjectMaterializer`1 )
       at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
       at System.Data.Linq.EntitySet`1.Load()
       at System.Data.Linq.EntitySet`1.get_Count()
       at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
       at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
       at MS.Internal.Data.EnumerableCollectionView.LoadSnapshotCore(IEnumerable source)
       at MS.Internal.Data.EnumerableCollectionView..ctor(IEnumerable source)
       at MS.Internal.Data.ViewManager.GetViewRecord(Object collection, CollectionViewSource cvs, Type collectionViewType, Boolean createView)
       at MS.Internal.Data.DataBindEngine.GetViewRecord(Object collection, CollectionViewSource key, Type collectionViewType, Boolean createView)
       at System.Windows.Data.CollectionViewSource.GetDefaultCollectionView(Object source, Boolean createView)
       at System.Windows.Data.CollectionViewSource.GetDefaultCollectionView(Object source, DependencyObject d)
       at System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable value)
       at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value)
       at ScScaleLoad.AppPages.NewTicket.JobListGrid_LoadingRowDetails(Object sender, DataGridRowDetailsEventArgs e) in C:\ace-co-devel\Aceco.NET\ScaleLoad (VS2010)\ScScaleLoad\AppPages\NewTicket.xaml.cs:line 1209
    System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value)
       at ScScaleLoad.AppPages.NewTicket.JobListGrid_LoadingRowDetails(Object sender, DataGridRowDetailsEventArgs e) in C:\ace-co-devel\Aceco.NET\ScaleLoad (VS2010)\ScScaleLoad\AppPages\NewTicket.xaml.cs:line 1209
    It would appear that there is some field of int32 data the the DataGrid is unhappy with, but I'll be damned if I can find it; there's no indication from the debugger as to what field of data it's working with and I'm not aware of any option to get to that info. And finally, since this is VS2010/.NET4.0, I can't step into the Framework because MS hasn't yet released all the necessary symbol files.

    There's another curriosity too: If I add the following to the SelectionChanged event ...

    int count = this.selectedOrder.Dsps.Count;
    ... so I can get a look at the Dsps Entity set before WPF tries to do anything with it, the Count property is throwing the exception as well. Now, if I hover my mouse above the Count so the debugger is forced to run the query (yes, it's deffered), and I do that N+1 times (where N is the number of Dsp records) the debugger tool tip displays the Exception N times and I can see in my SQL profiler the query being executed N+1 times. Then, when I hit F5 to run again, there is no Exception and WPF is happy as clam with the data getting thown at it.

    And one more thing, I'm sure it's some piece of data that the application is having a problem with because I have another test database that I can attach to and I don't see any of these problems. So, now you asking, "well why don't you compare the two sets of records from the two databases?" Well, I did that to and still can't see what it is.

    Please help, MS LINQ to SQL WPF guru's.


    Bill McCormick -- MSDN Subscriber
    Wednesday, February 10, 2010 10:41 PM

Answers

  • Looks like I have this solved. In the table where was having a problem, I found a smallint DB field that was getting mapped to an int property. Against a DB w/ compatibility mode set to SQL 2000, this seemed not to be a problem. It was a problem however with higher (2005,2008) compatability modes.

    A somewhat larger issue is that there was no information available in the exception handling system that eluded to which field there was a problem. The only way that I was able to find this was my manually comparing each database field type to the entity/db field type.

    Thanks


    Bill McCormick -- MSDN Subscriber
    • Marked as answer by Bill McCormick Wednesday, February 17, 2010 8:20 PM
    Wednesday, February 17, 2010 8:20 PM