Assign Image-field (SQL Server) to TImage

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

Stef MerlijnDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

developmentguruPresidentCommented:
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
Stef MerlijnDeveloperAuthor Commented:
Changing the IF-statement didn't resolve it.
I still get the AV
0
Stef MerlijnDeveloperAuthor Commented:
The AV occurs at line:
IFoto.Picture.Graphic.LoadFromStream(AStream);
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

developmentguruPresidentCommented:
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
Stef MerlijnDeveloperAuthor Commented:
The stream is of size 10805, so it contains something.
The AV still occurs...
0
developmentguruPresidentCommented:
Check each portion of the statement.  Is IFoto nil?  Is IFoto.Picture nil?  Is IFoto.Picture.Graphic nil?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stef MerlijnDeveloperAuthor Commented:
IFoto.Picture.Graphic was nil.
0
Stef MerlijnDeveloperAuthor Commented:
Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.