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.

Recommended Answers

All 3 Replies

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.

Try after line 28
29.ADOQuery1.Post;

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.