invalid cast exception

Posts   
 
    
aweber1
User
Posts: 7
Joined: 08-Apr-2005
# Posted on: 08-Apr-2005 01:06:49   

Hello,

I'm receiving a System.InvalidCastException when trying to populate an Entity or Collection. Not sure what's going on, but it seems as if a connection is not being created. I'm using an Oracle 10 db. I am able to connect to the same db in other apps using ODP.Net without problem, so I know my listener and connection string are working.

This is my first time using LLBLGen in development, so I have no idea what I could be doing wrong or where to go next. This seems like an awesome tool, just hoping I can get this issue worked out simple_smile Please help!

Here are two separate methods that lead to the exception: (i've tried both methods with and without data in the table in question)

HtjobsDepartmentsEntity hd = new HtjobsDepartmentsEntity(2);

OR

HtjobsDepartmentsCollection hdc = new HtjobsDepartmentsCollection();
hdc.GetMulti(null);

here's what my web.config appSettings looks like: (xx's replaced with real values of course)

<add key="Main.ConnectionString" value="data source=xxxxxx;User Id=xxxxxx;Password=xxxxxx;persist security info=false;"/>

here's the Stack Trace:

[InvalidCastException: Specified cast is not valid.]
   Oracle.DataAccess.Client.OracleCommand.System.Data.IDbCommand.set_Connection(IDbConnection value) +12
   SD.LLBLGen.Pro.ORMSupportClasses.RetrievalQuery..ctor(IDbConnection connectionToUse, IDbCommand commandToUse) +44
   SD.LLBLGen.Pro.DQE.Oracle.DynamicQueryEngine.CreateSelectDQ(IEntityFieldCore[] selectList, IFieldPersistenceInfo[] fieldsPersistenceInfo, IDbConnection connectionToUse, IPredicate selectFilter, Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, IRelationCollection relationsToWalk, Boolean allowDuplicates, IGroupByCollection groupByClause, Int32& uniqueMarker) +266
   SD.LLBLGen.Pro.DQE.Oracle.DynamicQueryEngine.CreateSelectDQ(IEntityFields selectList, IDbConnection connectionToUse, IPredicate selectFilter, Int64 maxNumberOfItemsToReturn, ISortExpression sortClauses, IGroupByCollection groupByClause) +89
   Hickorytech.Base.DaoClasses.HtjobsDepartmentsDAO.FetchHtjobsDepartments(IEntity entityToFetch, ITransaction containingTransaction, Int32 deptid, IPrefetchPath prefetchPathToUse) +231
   Hickorytech.Base.EntityClasses.HtjobsDepartmentsEntityBase.Fetch(Int32 deptid, IPrefetchPath prefetchPathToUse) +67
   Hickorytech.Base.EntityClasses.HtjobsDepartmentsEntityBase.InitClassFetch(Int32 deptid, HtjobsDepartmentsValidator validator, IPropertyDescriptorFactory propertyDescriptorFactoryToUse, IEntityFactory entityFactoryToUse, IPrefetchPath prefetchPathToUse) +55
   Hickorytech.Base.EntityClasses.HtjobsDepartmentsEntityBase..ctor(Int32 deptid) +64
   Hickorytech.Base.EntityClasses.HtjobsDepartmentsEntity..ctor(Int32 deptid) +5
   hickorytech._admin_.departments.Page_Load(Object sender, EventArgs e) in g:\inetpub\wwwroot\hickorytech\employment\_admin_\departments.aspx.cs:31
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Page.ProcessRequestMain() +750
jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 08-Apr-2005 05:28:18   

I would venture to guess that, as you are using Oracle, a typecast is occuring while setting properties in your entity class that is throwing this exception. Double check what .Net types your Oracle column types are mapping to, and also if you can examine the generated query to see what data is being returned (with Beta this kind of tracing is built in, with the Final version there are extended Adapter templates to add this functionality).

The problem, as I can tell you from experience, is probably that your Oracle types don't map cleanly to .Net types so check those to be sure. I think Frans plans to release a type conversion component in the near future, but for now some tweaking has to be done in your database.

jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 08-Apr-2005 05:31:14   

I also just thought about this, but do you get this exeption when doing any kind of call tothe database through the code? Try executing a simple scalar query or something of the sort..if you still get it then you may have some mismatched code. What driver are you using in the project?

aweber1
User
Posts: 7
Joined: 08-Apr-2005
# Posted on: 08-Apr-2005 07:03:58   

ok, i tried executing a simple scalar query and got the same exception. The driver I'm using for this project is the 'Oracle 10g Driver [ODP.NET 10.1.x]'. I'm going to check out your other suggestion as well and see if the datatypes match and try to get the query as well. Thanks!

aweber1
User
Posts: 7
Joined: 08-Apr-2005
# Posted on: 08-Apr-2005 07:05:48   

one other thing i just thought of... should i be using the Adapter scenario for generation? I specified the two-class scenario... maybe i'll give that a go as well

aweber1
User
Posts: 7
Joined: 08-Apr-2005
# Posted on: 08-Apr-2005 07:47:23   

so i checked out the adapter generation and got it things to work, kind of... I am able to retrieve a single entity now, but only one of the entity fields contains a value, a number. The other two fields are set to VARCHAR2 in the db, and are cast as System.String in the base class. I can access those fields, but they have no current value, though there is a value specified in the db. I printed out the XML of the entity returned, and sure enough, the <currentvalue> node of each of those fields is empty.

Is there a datatype besides VARCHAR2 I should be using?

also, although the single entity functions kind of work, the EntityCollection functions still throw an invalid cast exception.

hmm, time to sleep on it...

jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 08-Apr-2005 14:38:33   

aweber1 wrote:

one other thing i just thought of... should i be using the Adapter scenario for generation? I specified the two-class scenario... maybe i'll give that a go as well

Sorry, it was late and I wasn't thinking straight--if I had looked closer I would have noticed you were using a GetMulti() function, which is SelfServicing. No the 2 class scenario is fine, but I am surprised you are getting these problems when retrieving entities. Make sure that when you generate the code, do it in a clean directory (just to be certain). Are you using the trial version or the full version? If you are using the full version, perhaps you can try to download updated runtime libraries and drivers. I never had a problem using self servicing, but I'll admit that I use Adapter in all projects because I prefer the greater control over database connections, etc.

jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 08-Apr-2005 15:00:39   

aweber1 wrote:

so i checked out the adapter generation and got it things to work, kind of... I am able to retrieve a single entity now, but only one of the entity fields contains a value, a number. The other two fields are set to VARCHAR2 in the db, and are cast as System.String in the base class. I can access those fields, but they have no current value, though there is a value specified in the db. I printed out the XML of the entity returned, and sure enough, the <currentvalue> node of each of those fields is empty.

Can you post the code where you retrieve the entity (Adapter)? Soemthign strange is going on here if only certain fields are being populated--I've never run into that problem before.

aweber1 wrote:

Is there a datatype besides VARCHAR2 I should be using?

No, VARCHAR2 is fine, that maps to System.String with no problems that I'm aware of.

aweber1
User
Posts: 7
Joined: 08-Apr-2005
# Posted on: 08-Apr-2005 18:58:26   

i appreciate all your help in this matter!

I'm running the Full version of the software, with all the updated drivers and runtimes. Here's the code I'm using to access the Entity:


// 2 is the primary key value of the row i'm selecting
HtjobsDepartmentsEntity hd = new HtjobsDepartmentsEntity(2);
string blee = String.Empty;
hd.WriteXml(out blee);
HttpContext.Current.Response.Write("<xmp>" + blee + "</xmp>");
HttpContext.Current.Response.Write(hd.Deptid + " " + hd.DeptName + " " + hd.ShortDesc);

the last two lines are just simple writes so i can see what's in the entity values. Is there something I should be doing to populate the entity besides passing the primary key in the constructor? I guess I was just going off the code samples in the help guide.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 09-Apr-2005 11:33:58   

Which ODP.NET version are you using? Only Final non-beta versions are supported, which are: ODP.NET for 9i: 9.2.0.401 and ODP.NET for 10g: 10.1.0.3.01

ANY other version is a beta, as these are the versions which are distributed by Oracle on the OTN website. if you're using Oracle's 10g beta driver 10.1.0.3.03, please install the final driver for 10g: 10.1.0.3.01

Thanks for your help on this, Larkin simple_smile

Frans Bouma | Lead developer LLBLGen Pro
aweber1
User
Posts: 7
Joined: 08-Apr-2005
# Posted on: 09-Apr-2005 18:37:27   

i'm using ODP.NET 10.1.0.3.01

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 09-Apr-2005 19:08:42   

aweber1 wrote:

i'm using ODP.NET 10.1.0.3.01

Very strange. The problem is that the OracleCommand object gets an OracleConnection assigned which is of a different type, i.e. a different ODP.NET type (or different database type altogether).

I'm not sure if you've made a mistake in the references in the generated vs.net project(s). Could you check that for me please, that these are all .NET 1.1 (or all .NET 1.0) and for 10g, and that you didn't by accident reference the 9i DQE for example?

Frans Bouma | Lead developer LLBLGen Pro
jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 09-Apr-2005 23:14:10   

Otis wrote:

Thanks for your help on this, Larkin simple_smile

No problem, I'm no substitute for you Frans but I thought I could help narrow down the problem. I agree with you on the wrong OracleConnection object being returned--that was my next guess, though if he is using the correct drivers that is indeed strange, but I'm guessing that this is the case.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-Apr-2005 13:43:21   

jookyone wrote:

Otis wrote:

Thanks for your help on this, Larkin simple_smile

No problem, I'm no substitute for you Frans but I thought I could help narrow down the problem. I agree with you on the wrong OracleConnection object being returned--that was my next guess, though if he is using the correct drivers that is indeed strange, but I'm guessing that this is the case.

simple_smile I've seen the error before, and it then did originate from a bad oracle ODP.NET version (9.2.0.410, which is pulled from the OTN website since, and it never got released again, though it did contain bugfixes).

Frans Bouma | Lead developer LLBLGen Pro
jookyone avatar
jookyone
User
Posts: 104
Joined: 25-Jan-2005
# Posted on: 10-Apr-2005 16:42:20   

Ahh, that makes sense. I hope that fixes the problem for you aweber1 simple_smile

aweber1
User
Posts: 7
Joined: 08-Apr-2005
# Posted on: 11-Apr-2005 07:43:57   

finally got it worked out! it was the driver version mismatch. My dev box has version 10.1.0.301 of ODP.NET, which I'm referencing in VS.NET, however the production box has 10.1.0.2 I specifically asked the sysadmin on 2 separate occasions if he was absolutely positive 10.1.0.301 was installed. Finally made him prove it to me and sure enough, the older version was installed.

You guys have been great, helping out a newbie and being extremely patient. Thank you very much! simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 11-Apr-2005 09:09:26   

Glad it's solved! simple_smile

Frans Bouma | Lead developer LLBLGen Pro