Wrox Programmer Forums
|
BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio
This is the forum to discuss the Wrox book ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solution by Vincent Varallo; ISBN: 9780470396865
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 1st, 2009, 11:14 AM
Authorized User
 
Join Date: Mar 2009
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default Invaild cast Exception

Hello,

I'm in the ENTmenuItemBO Class and I'm getting this error: (BTW this is written in vb.net)

Unable to cast object of type 'V2.PaidTimeOff.DAL.ENTmenuItem' to type 'V2.PaidTimeOff.DAL.Framework.IENTBaseEntity'

I will admit interfaces are my weak spot, so maybe I'm just doing something wrong.

The code that trigger the error is here:

Code:
 
For Each menuItem In menuItems
     menuItemBO = New ENTMenuItemBO
     menuItemBO.MapEntityToProperties(menuItem)
     ...
At this point I have a new ENTMenuItem Class (looks like a record, from the table ENTMenuItem) I pass this object to MapEntityToProperties which takes an IENTBaseEntity interface object. Which I'm not passing it, hence the casting error.

Here's the Sub for MapEntityToProperties:
Code:
 
Public Sub MapEntityToProperties(ByVal entity As IENTBaseEntity)
   _CreateDate = entity.CreateDate
   _CreateENTUserAccountId = entity.CreateENTUserAccountId
   _ModifyDate = entity.ModifyDate
   _ModifyENTUserAccountId = entity.ModifyENTUserAccountId
   _Version = entity.Version
   MapEntityToCustomProperties(entity)
End Sub
Here's the Interface object:

Code:
 
Public Interface IENTBaseEntity
   Property CreateDate() As Date
   Property CreateENTUserAccountId() As Integer
   Property ModifyDate() As Date
   Property ModifyENTUserAccountId() As Integer
   Property Version() As Binary
End Interface
Thanks,
-realkewl

Last edited by realkewl; April 1st, 2009 at 11:27 AM..
 
Old April 1st, 2009, 06:00 PM
Wrox Author
 
Join Date: Jan 2009
Posts: 73
Thanks: 0
Thanked 7 Times in 7 Posts
Default

In the DAL project there should be a file called CustomizedEntities.cs or if you switched to VB then .VB.
You need to declare a partial class in this file and name it the same as the entity class that was created by the ORM designer. The class must implement the IENTBaseEntity interface.

Code:
    public partial class ENTMenuItem : IENTBaseEntity { }
 
Old April 2nd, 2009, 09:26 AM
Authorized User
 
Join Date: Mar 2009
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hey Vince,

Yes, I do have the CustomizedEntities.vb class and inside that a partial class see below:

Code:
Partial Public Class ENTmenuItem
Implements IENTBaseEntity
Dim _CreateDate AsDate
Dim _CreateENTUserAccountId AsInteger
Dim _ModifyDate AsDate
Dim _ModifyENTUserAccountId AsInteger
Dim _Version As Binary
 
Public Property CreateDate() As Date Implements Framework.IENTBaseEntity.CreateDate
Get
Return _CreateDate
EndGet
Set(ByVal value AsDate)
_CreateDate = value
EndSet
End Property

 
Public Property CreateENTUserAccountId() As Integer Implements Framework.IENTBaseEntity.CreateENTUserAccountId
Get
Return _CreateENTUserAccountId
EndGet
Set(ByVal value AsInteger)
_CreateENTUserAccountId = value
EndSet
End Property

Public Property ModifyDate() As Date Implements Framework.IENTBaseEntity.ModifyDate
Get
Return _ModifyDate
EndGet
Set(ByVal value AsDate)
_ModifyDate = value
EndSet
End Property

 
Public Property ModifyENTUserAccountId() As Integer Implements Framework.IENTBaseEntity.ModifyENTUserAccountId
Get
Return _ModifyENTUserAccountId
EndGet
Set(ByVal value AsInteger)
_ModifyENTUserAccountId = value
EndSet
End Property

 
Public Property Version() As System.Data.Linq.Binary Implements Framework.IENTBaseEntity.Version
Get
Return _Version
EndGet
Set(ByVal value As System.Data.Linq.Binary)
_Version = value
EndSet
End Property

 
End Class
Thanks,
-realkewl
 
Old April 2nd, 2009, 03:14 PM
Authorized User
 
Join Date: Mar 2009
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Update...here is what I did to get around the issue for now:

I updated both MapEntityToProperties and MapEntityToCustomProperties
to accept an object instead of the interface.

e.g.
Code:
 
Public Sub MapEntityToProperties(ByVal entity As Object)


The code runs but the menu does not look like a menu...it's still early so I will continue with the code and see how it shapes up.

Please let me know if you find a reason why the casting from class to interface IENTBaseEntity does not seem to work.

Thanks,
-realkewl
 
Old April 4th, 2009, 12:09 PM
Wrox Author
 
Join Date: Jan 2009
Posts: 73
Thanks: 0
Thanked 7 Times in 7 Posts
Default

The original sample code used UpdateDate and UpdateENTUserAccountId. I noticed your sample code used "ModifyDate". Was your base class changed and were the fields in the table changed too so the Entity object created by the ORM Designer uses Modify instead of Update?

Code:
public interface IENTBaseEntity
    {
        DateTime InsertDate { get; set; }
        int InsertENTUserAccountId { get; set; }
        DateTime UpdateDate { get; set; }
        int UpdateENTUserAccountId { get; set; }
        Binary Version { get; set; }   
    }
 
Old April 7th, 2009, 09:36 AM
Authorized User
 
Join Date: Mar 2009
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hey Vince,

Yes, that is correct, the base class and the table were modified to reflect ModifyDate...and some of the other naming conventions. I did this so I could learn a little better.

Thanks,
-realkewl





Similar Threads
Thread Thread Starter Forum Replies Last Post
Linq: 'Specified cast is not valid' error exception. mksingh Visual Studio 2008 2 February 4th, 2009 11:06 PM
invalisd cast msinha199 ASP.NET 2.0 Professional 3 May 28th, 2008 07:08 AM
Dynamic Cast in C# jacob C# 15 September 25th, 2006 11:37 AM
Invaild use of Me keyword jakeone Beginning VB 6 3 September 28th, 2004 10:53 AM
invalid cast exception giulio_santorini C# 2 August 21st, 2003 08:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.