1,105,295 Community Members

Saving and loading jpeg images to database

Member Avatar
Fireprufe15
Junior Poster in Training
52 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm trying to make a program that saves images in a database. I've pulled together some info on the net and wrote up code that I think is supposed to work, but fails in a completely unique way on either side (the saving and the loading).

Here's my code, could you tell me what I'm doing wrong?

Load from Database, gives me a memory address access violation error:

procedure TfrmMain.btnOpenProfileT4Click(Sender: TObject);
var
  S : TMemoryStream;
  ikode : integer;
begin

  S := TMemoryStream.Create;
    ikode := cbxDeelnemerT4.ItemIndex + 1;
  S := TMemoryStream.Create;
  ADOQuery1.Close;
  ADOQuery1.SQL.Text := 'SELECT * FROM DEELNEMERS WHERE Kode = '+inttostr(ikode);
  ADOQuery1.Open;
  try
    TBlobField(ADOQuery1.FieldByName('Foto')).SaveToStream(S);
    S.Position := 0;
    Image1.Picture.Graphic.LoadFromStream(S);

  finally
   s.Free;
  end;    
end;

Saving to the database. This code doesn't give an error as such, but it just doesn't save the image to the database:

procedure TfrmMain.btnAddPickT4Click(Sender: TObject);
var
  S : TMemoryStream;
  ikode : integer;
begin

  if cbxDeelnemerT4.ItemIndex < 0 then
  begin
    MessageDlg('Kies asseblief ''deelnemer!',mtInformation,[mbOK],1);
    Exit;
  end;

  if OpenPictureDialog1.Execute then
    if FileExists(OpenPictureDialog1.FileName) then
      Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName)
    else
      MessageDlg('Die lêer bestaan nie!',mtError,[mbOK],1);

  ikode := cbxDeelnemerT4.ItemIndex + 1;
  S := TMemoryStream.Create;
  ADOQuery1.Close;
  ADOQuery1.SQL.Text := 'SELECT * FROM DEELNEMERS WHERE Kode = '+inttostr(ikode);
  ADOQuery1.Open;
  try
    Image1.Picture.Graphic.SaveToStream(S);
    S.Position := 0;
    ADOQuery1.Insert;
    TBlobField(ADOQuery1.FieldByName('Foto')).LoadFromStream(S);
  finally
    S.Free;
  end;


end;

Any help is greatly appreciated.

Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,829 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
0
 

Load from Database, gives me a memory address access violation error:

On what line?

For the insert, it looks like you are mixing the syntax for an ADOQuery with an ADOTable. I need to test this though, will have to reproduce somehow to be more specific.

Member Avatar
riki.kavadarci
Newbie Poster
4 posts since Jun 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Try after line 28
29.ADOQuery1.Post;

Hemo2013
Newbie Poster
1 post since Oct 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

the problem is not in the code.
the database you are using not support save jpeg image.
for example Access not support saving jpg image.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: