Invalid class typecast

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
luis_augusto
Posts: 43
Joined: Fri 14 Oct 2005 13:45

Invalid class typecast

Post by luis_augusto » Fri 26 Aug 2011 21:22

I´ve upgraded from sdac 2.79 to unidac 3.5 and, since then, I am having trouble with the following:


field1 = varchar(15)
field2 = text
field3 = text

when I try to do this, after opening the Tuniquery:

begin
Uniquery1.sql.text:='Select field1,field2,field3 from tablea'

tt:=tstringlist.create;
uniquery1.open;

tt.settext(pchar(Uniquery1.fieldbyname('field2').asstring)); <=== Error 'Invalid class typecast'
Uniquery1.close;
end;

I am using MSSQL 2008 and Unidac 3.70.0.19 for Delphi7
This routine is inside a dll

AndreyZ

Post by AndreyZ » Tue 30 Aug 2011 11:00

Hello,

I cannot reproduce the problem. Please try creating a small sample to demonstrate the problem and send it to andreyz*devart*com.

luis_augusto
Posts: 43
Joined: Fri 14 Oct 2005 13:45

workaround to "invalid class typecast"

Post by luis_augusto » Tue 30 Aug 2011 13:10

Hello,

It seems the fact that the routine is inside a dll is critical to raise this exception.

I substitute Query.fieldbyname('fieldname').asstring for tmemdataset(Query).getblob('fieldname').asstring, then the problem was solved.
Nevertheless, in a system with a huge amount of code,this problem can turn the program into a "mined field", since one would have to find every dll where this technique was used to change it.
your customer´s the legacy, when changing the behaviour of your code. Mainly, because people who is interested in a kind of software like yours is also interested in the preservation of its own legacy.

That been said, Unidac rocks!

AndreyZ

Post by AndreyZ » Wed 31 Aug 2011 11:54

I used your code in a dll, and there were no problems. We are interested in investigating this problem. Please try composing a small sample to demonstrate the problem and send it to andreyz*devart*com, including a script to create and fill the tablea table. We are looking forward to receiving your sample.

luis_augusto
Posts: 43
Joined: Fri 14 Oct 2005 13:45

Sample sent

Post by luis_augusto » Wed 31 Aug 2011 15:39

I sent you a sample. It appears to be related with the origin of the connection. If the connection is created inside the dll there is no problem, but if you use a TUniconnection as a parameter for the external procedure and use it to open a TUNIquery inside the dll, the exception raises.

AndreyZ

Post by AndreyZ » Thu 01 Sep 2011 07:10

You are right, this problem is caused by using a connection that was created in an application inside dll. In this case the "as" operator is used that causes invalid type cast error. To solve the problem, you can use one of the following ways:
- create and configure TUniConnection inside dll;
- create TUniConnection inside dll and use the AssignConnect method to share database connection. I've sent you your sample with my corrections. It demonstrates this approach.

luis_augusto
Posts: 43
Joined: Fri 14 Oct 2005 13:45

Post by luis_augusto » Thu 01 Sep 2011 13:35

yes, this solution works fine for me. Thanks a lot.

Post Reply