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

[Solved] Exception during type casting, on binding time.

2 Answers 209 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Patryk Kubiela
Top achievements
Rank 1
Patryk Kubiela asked on 05 Aug 2011, 07:56 PM

Hi,
 Before we give the basic question, I will describe what and how.

Technologies which use:

  • NRoute;
  • NHibernate;
  • RIA Services;
  • SQL Server;

I also use Telerik controls.


This Silverlight project looks like this:

Database- SQL Server;
nHibernate - mapping:

<?xml version="1.0" encoding="utf-8"?>
    <hibernate-mapping assembly="Wspolny" namespace="Model.Entities.Promocje" xmlns="urn:nhibernate-mapping-2.2" default-lazy="true">
      <class name="EPromocja" table="Promocja" lazy="1" >
        <id name="ID">
          <generator class="identity" />
        </id>
 
        <property name="Nazwa">
          <column name="Nazwa" sql-type="nvarchar" not-null="true" />
        </property>
 
        <property name="Opis">
          <column name="Opis" sql-type="nvarchar" not-null="false" />
        </property>
 
        <property name="MiejsceSprawdzania">
          <column name="MiejsceSprawdzania" sql-type="int" not-null="false" />
        </property>
 
        <property name ="OperatorOpiekun">
          <column name="OperatorOpiekun" sql-type="int" not-null="false"/>
        </property>
      </class>
    </hibernate-mapping>

nHibernate – Entity:

[KnownType(typeof(PromocjaZDatami))]
public class EPromocja : EBazowa
        {
 
            public virtual string Nazwa { get; set; }
 
            public virtual string Opis { get; set; }
 
 
            public virtual int? MiejsceSprawdzania { get; set; }
 
            public virtual int OperatorOpiekun { get; set; }
        }
 
        public class PromocjaZDatami : EPromocja
        {
            public virtual DateTime? DataOd { get; set; }
 
            public virtual DateTime? DataDo { get; set; }
        }

Binding to RadGridView look's like this:

<telerik:RadGridView  SelectedItem="{Binding ZaznaczonaPromocja, Mode=TwoWay}"
 ItemsSource="{Binding Path=ClientDataSource.DataView}"> ...

And ClientDataSource...

public RadDomainDataSource ClientDataSource
        {
            get
            {
                if (_zrodloDanych == null)
                {
                    _zrodloDanych = new RadDomainDataSource();
                    _zrodloDanych.DomainContext = this.DomainContext;
                    _zrodloDanych.AutoLoad = false;
 
                    _zrodloDanych.SortDescriptors.Add(new Telerik.Windows.Data.SortDescriptor()
                    {
                        Member = "Nazwa",
                        SortDirection = ListSortDirection.Ascending
                    });
 
                    _zrodloDanych.SortDescriptors.Add(new Telerik.Windows.Data.SortDescriptor()
                    {
                        Member = "DataOd",
                        SortDirection = ListSortDirection.Ascending
                    });
 
                    _zrodloDanych.SortDescriptors.Add(new Telerik.Windows.Data.SortDescriptor()
                    {
                        Member = "DataDo",
                        SortDirection = ListSortDirection.Ascending
                    });
 
                    _zrodloDanych.LoadingData += (s, e) =>
                    {
                        CzyWczytujeDane = true;
                        e.LoadBehavior = LoadBehavior.RefreshCurrent;
                    };
 
                    _zrodloDanych.LoadedData += (s, e) =>
                    {
                        CzyWczytujeDane = false;
                        if (Promocja != null)
                            ZaznaczonaPromocja = Promocja;
                    };
 
                    _zrodloDanych.QueryName = "PobierzPromocjeZDatami";
                    //_zrodloDanych.QueryName = "PobierzPromocje";
                }
 
                return _zrodloDanych;
            }
            set
            {
                _zrodloDanych = value;
                NotifyPropertyChanged(() => ClientDataSource);
            }
        }

And I know that, PobierzPromocjaZDatamialways returns the correct data set.

The problem is that when I start a project, the data is correctly loaded and displayed in RadGridView. But when I added the object EPromocja, and I use SubmitChanges() function, that data is properly collected (they look the same as before, only one object more), but can not be properly displayed.

I get exception in Just-In_Time Debugger:

An unhandled exceptioin ('Unhandled Error in Silverlight Application Code: 4004 Category: ManagedRuntimeError Message: System.InvalidCastException: Unable to cast object of type 'Model.Entities.Promocje.EPromocja' to type 'Wspolny.DAL.DTO.PromocjaZDatami'.)

I do not know how to bite. Please help!

Regards, Patryk

2 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 08 Aug 2011, 10:14 AM
Hi Patryk,

 Can you post the stack trace? Do you get this exception in our (Telerik) code client-side or server-side?

Best wishes,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Patryk Kubiela
Top achievements
Rank 1
answered on 08 Aug 2011, 11:25 AM
Hi Vlad,

PobierzPromocjaZDatami send the data set to client, and i got that error:

https://picasaweb.google.com/lh/photo/trl88DzNaCp7o6k6or-2qg?feat=directlink

Sorry for that form of presentation, but I did not know how to do it differently.

Regards,
Patryk.

Tags
GridView
Asked by
Patryk Kubiela
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Patryk Kubiela
Top achievements
Rank 1
Share this question
or