Solved

Invalida class Typecast

Posted on 2004-09-07
25
1,517 Views
Last Modified: 2008-01-09
Hi everyone,

I'm developing a database application on delphi 7 and Microsoft Access.
I have a memo field in a table called 'requrimientos' and in my form a dbgrid showing this table. There is a TMemo in the form wich i want to put the info from the dbmemo field everytime the user clicks on a cell in the dbgrid.

This is my source:

procedure TForm1.DBGrid1CellClick(Column: TColumn);
var
  memo: TBlobStream;
  Aux : TMemo;
begin
  memo := TBlobStream.Create(Conex.Juegosrequrimientos, bmRead);
  Memo1.Lines.LoadFromStream(memo);
end;

Where 'Conex' it's a DataModule where i have all de objects related to the database.

The project is well compiled, but the problem is at runtime, when i click on a cell i get a 'Invalid class typecast' error.

Someone knows why?

Thanks in advance.
0
Comment
Question by:ttrobin
  • 7
  • 6
  • 5
  • +3
25 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12001360
why your field is BLOB? It the field should contain only text, it can be defined as TEXT. Then all you have to do is to change TMemo with TDBMemo and attach it to the same DataSet as the DBGrid...
0
 

Author Comment

by:ttrobin
ID: 12001496
hi Ivanov_G,

it must have several lines of text, not only one, that's the reason.
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 12001611
is Conex.Juegosrequrimientos a blob field?

if not, that's the problem... you can try something like:

 memo := TBlobStream.Create(TBlobField(Conex.Juegosrequrimientos), bmRead);
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:ttrobin
ID: 12001844
Hi BlackTigerX,

yes it is a TBLobField, in fact its a Memo access field.

Anyway i have tried to put it as OLE field, and use CreateBLobStream method, but nothing its shown in the memo field in the form1 ... And of course, the fields in the data base contains data :)
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 12003319
you can always read it as a string with .AsString :)
Memo1.Lines.Text:=Conex.Juegosrequrimientos.AsString;
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12003466
why not use a tdbmemo-component?
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12003484
TEXT is not VARCHAR... use can have several lines of text...
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 12003514
what ? and a varchar can't have crlf's ? is that what you're implying ?
the only difference is internal handling .. you can't sort, group, query on blobs .. and they are differently allocated than varchars
especialy in ms access !
AFAIK a text field in access is max 255 chars large, TEXT is 64k bytes max .. binary can be larger
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12003528
>and a varchar can't have crlf's ?
a varchar can have crlf's, as any other string-type
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 12003553
well I know that :)
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12004017
Hi guys :)

I'd also use a TDBMemo, btw, as the question is about the Invalid Typecast, it must be solved with the correct syntax as follows:

memo := TBlobStream(Juegos.CreateBlobStream(Juegosrequrimientos,bmread)); //The tream must be created by the table
Memo1.Lines.LoadFromStream(memo);

0
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 50 total points
ID: 12004022
missed the Conex

memo := TBlobStream(Conex.Juegos.CreateBlobStream(Conex.Juegosrequrimientos,bmread));
0
 

Author Comment

by:ttrobin
ID: 12005327
Hi Ferruccio68,

I have tried what u said to me, and already dong get invalid class typecast message but nothing is shown in the TMemo object in the form ...

Maybe i have missing something ...
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12005401
btw, why a tblobstream?

var
  ms : TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    TBlobField(DataSet.FieldByName('BlobFieldName')).SaveToStream(ms);
    ms.position := 0; //spool back
    memo1.lines.loadfromstream(ms);
  finally
    ms.free;
  end;
end;

just as alternative, just from head

meikl ;-)
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12005431
--> btw, why a tblobstream?
because he's declared it in db as MEMO and become of type TMemoField on Dataset....

--> nothing is shown in the TMemo object
are you sure that there are datas in the tested record? for me it's working fine....

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12005447
>because he's declared it in db as MEMO and become of type TMemoField on >Dataset....

?? may i have overlooked this

memo1.lines.text := TMemoField(DataSet.FieldByName('MemoFieldName')).AsText;

meikl ;-)
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12005473
--> ?? may i have overlooked this
No meikl, it's just my guess. He said 'yes it is a TBLobField, in fact its a Memo access field.' but i think it's his mistake, beacuse AFAIK a MEMO Access field is treated as TMemoField, non Tblobfield

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12005493
>is treated as TMemoField, non Tblobfield
well, then this should be the invalid typecast, i guess
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12005524
Yes, in fact, and to solve it as he was asking for the right syntax was the posted by me above. BTW i repeat: i'd use a TDBMemo (why puzzling your mind with a Tmemo?). Of course other methods like yours should work well.

F68 ;-)
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 12005582
and about my first comment ? it'll work regardless of the fieldtype and is applicable anywhere
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12005687
yep, lee, should be applicable

btw. asText as i suggested above is not correct (asString would be correct)

meikl ;-)
0
 

Author Comment

by:ttrobin
ID: 12006858
Ok, i dont understand nothing at all :)

I have data written in this field on the database, i can see it when i open the table from access, but however nothing its shown in the memo field, or even the dbmemo field at the program.

I tried the following: put in the same form all fields from the table called 'Games', and i edited the dbmemo field at runtime, saving the information i wrote. And it works!!!

Then, why the information contained in the field (that exists and its visible from access) its not showed in the dbmemo field even when i put it directly in the form?

Thanks in advance for your time to everyone.
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 12007105
I gues that these memo fields were edited directly in access, maybe pasting text directly from some editor....
If so try using a Tdbrichedit.....
0
 

Author Comment

by:ttrobin
ID: 12007942
Hi Ferruccio68,

Yes, they were, and the tdbrichEditText didn't work :)

I think i will have to put the info again through the program, copying and pasting ...

Anyway, if anyone knows why it happens, please explain it!

Thanks to all people that tried or wanted to help me.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12008238
>I gues that these memo fields were edited directly in access
>Anyway, if anyone knows why it happens, please explain it!

in this case an ole-information is prefixed to the real content,
somewhere is a site, where the needed offset is discussed
(was for an image-type, i guess)

try to find it again

meikl ;-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Path  to current project in Delphi. 2 79
Run video youtube webbrowse 10 61
JAudiorecorder record freezing the app 29 67
CheckListBox usage 3 58
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now