Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Assign Image-field (SQL Server) to TImage

Posted on 2008-11-17
8
Medium Priority
?
1,094 Views
Last Modified: 2013-11-23
Hi,

After converting a MS Access database to SQL Server, I want to show the stored image (JPG) in a TImage.
The conversion was done like this:
if (Veld.DataType = ftBlob) then
              ADOTableSQLServer.FieldByName('Photo').asVariant := ADOTableAccess.FieldByName('Photo').AsVariant;

Assigning the image to the TImage is done like this (see below):
This code gives an AV: Access violation at address 00A4A936 in module 'MyApp.exe'. Read of address 00000000.

My questions:
1) Is the conversion done correctly like this? There do seem to be data in the SQL Server Photo-field.
2) Can anybody give me some suggestion on how to correct this?

  if NOT ((DM.aspRelatiesSelect.FieldByName('PHOTO').AsVariant) = Null) AND
         ((DM.aspRelatiesSelect.FieldByName('PHOTO').AsVariant) <> '') then
  begin
    AStream := TMemoryStream.Create;
    try
      TGraphicField(DM.aspRelatiesSelect.FieldByName('PHOTO')).SaveToStream(AStream);
      AStream.Position := 0;
      IFoto.Picture.Graphic.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  end;

Open in new window

0
Comment
Question by:Stef Merlijn
  • 5
  • 3
8 Comments
 
LVL 21

Assisted Solution

by:developmentguru
developmentguru earned 2000 total points
ID: 22977366
It looks to me like your code has an issue in the IF statement...  Try it more like this and let me know if you still get the error.
  if NOT (
    (DM.aspRelatiesSelect.FieldByName('PHOTO').IsNull) OR
    ((DM.aspRelatiesSelect.FieldByName('PHOTO').AsVariant) = '')
    ) then
    begin
      AStream := TMemoryStream.Create;
      try
        TGraphicField(DM.aspRelatiesSelect.FieldByName('PHOTO')).SaveToStream(AStream);
        AStream.Position := 0;
        IFoto.Picture.Graphic.LoadFromStream(AStream);
      finally
        AStream.Free;
      end;
    end;

Open in new window

0
 

Author Comment

by:Stef Merlijn
ID: 22979141
Changing the IF-statement didn't resolve it.
I still get the AV
0
 

Author Comment

by:Stef Merlijn
ID: 22982972
The AV occurs at line:
IFoto.Picture.Graphic.LoadFromStream(AStream);
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 21

Assisted Solution

by:developmentguru
developmentguru earned 2000 total points
ID: 22985442
Did you check to see if the stream contained anything?  Try the following after you SaveToStream.
if AStream.Size > 0 then
  begin
    AStream.Position := 0;
    IFoto.Picture.Graphic.LoadFromStream(AStream);
  end;

Open in new window

0
 

Author Comment

by:Stef Merlijn
ID: 22985860
The stream is of size 10805, so it contains something.
The AV still occurs...
0
 
LVL 21

Accepted Solution

by:
developmentguru earned 2000 total points
ID: 22989391
Check each portion of the statement.  Is IFoto nil?  Is IFoto.Picture nil?  Is IFoto.Picture.Graphic nil?
0
 

Author Comment

by:Stef Merlijn
ID: 23028528
IFoto.Picture.Graphic was nil.
0
 

Author Closing Comment

by:Stef Merlijn
ID: 31517424
Thank you!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Screencast - Getting to Know the Pipeline
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

580 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