I tried this code to save some fields and an image. I use MySQL and zzeos for connectinf to the database.

How to fix this code ?

procedure Tfbiodata.btnSaveClick(Sender: TObject);
var
  gambar : TMemoryStream;
begin
  if (edtnis.Text='') or (edtname.Text='') or (cmbjk.Text='') or (edtempat.Text='') or (edtgl.Text='') or (cmbtingkatan.Text='') then
  begin
    ShowMessage('Maaf !!! Data Anda Belum Lengkap ....');
    exit;
  end;
  begin
    zbiodata2.Open;
    zbiodata2.Append;
    zbiodata2.FieldByName('NIS').AsString := edtnis.Text;
    zbiodata2.FieldByName('Nama_siswa').AsString := edtname.Text;
    zbiodata2.FieldByName('Jenis_kelamin').AsString := cmbjk.Text;
    zbiodata2.FieldByName('Tempat_lahir').AsString :=  edtempat.Text;
    zbiodata2.FieldByName('Tanggal_lahir').AsString  := edtgl.Text;
    zbiodata2.FieldByName('Tingkatan').AsString :=  cmbtingkatan.Text;
    zbiodata2.FieldByName('Hasil_indentifkasi').AsString := lblhasil.Caption;
    zbiodata2.FieldByName('Metode_pembeaaran').AsString := memo1.Text;
    try
      convertobmp(openpicture.FileName);
      gambar := TMemorystream.Create;
      image1.Picture.Graphic.SaveToStream(gambar);
      zbiodata2.SQL.Text := 'insert into biodata (gambar) values (:p0)';
      zbiodata2.Params[0].LoadFromStream(gambar,ftBlob);
      zbiodata2.Post;    
      zbiodata2.ExecSQL;
    except
      on E:Exception do
        ShowMessage('sorry this a problem .' + #13 + 'Error : ' + E.Message);
    end;
  end;
end;

When I run this code, I get the error "sorry this is a problem . Error: List index out of bounds(2)"

有帮助吗?

解决方案

After calling image1.Picture.Graphic.SaveToStream(gambar), set gambar.Position back to 0 before then calling zbiodata2.Params[0].LoadFromStream(gambar,ftBlob):

image1.Picture.Graphic.SaveToStream(gambar);
gambar.Position := 0; // <-- add this
zbiodata2.Params[0].LoadFromStream(gambar,ftBlob);

With that said, you are using zbiodata2 for two different operations at the same time - editing a new row that is being appended, and executing an SQL statement. Don't do that! Use separate components for each operation.

If the image is being saved into the same row that is being appended, don't bother executing a separate SQL INSERT statement at all. Save the image data directly into the row's gambar TField before then calling zbiodata2.Post():

procedure Tfbiodata.btnSaveClick(Sender: TObject);
var
  gambar : TStream;
begin
  if (edtnis.Text='') or (edtname.Text='') or (cmbjk.Text='') or (edtempat.Text='') or (edtgl.Text='') or (cmbtingkatan.Text='') then
  begin
    ShowMessage('Maaf !!! Data Anda Belum Lengkap ....');
    Exit;
  end;

  try
    convertobmp(openpicture.FileName);

    zbiodata2.Open;
    zbiodata2.Append;
    try
      zbiodata2.FieldByName('NIS').AsString := edtnis.Text;
      zbiodata2.FieldByName('Nama_siswa').AsString := edtname.Text;
      zbiodata2.FieldByName('Jenis_kelamin').AsString := cmbjk.Text;
      zbiodata2.FieldByName('Tempat_lahir').AsString :=  edtempat.Text;
      zbiodata2.FieldByName('Tanggal_lahir').AsString  := edtgl.Text;
      zbiodata2.FieldByName('Tingkatan').AsString :=  cmbtingkatan.Text;
      zbiodata2.FieldByName('Hasil_indentifkasi').AsString := lblhasil.Caption;
      zbiodata2.FieldByName('Metode_pembeaaran').AsString := memo1.Text;

      if (image1.Picture.Graphic <> nil) and (not image1.Picture.Graphic.Empty) then
      begin
        gambar := TMemoryStream.Create;
        try
          image1.Picture.Graphic.SaveToStream(gambar);
          gambar.Position := 0;
          (zbiodata2.FieldByName('gambar') as TBlobField).LoadFromStream(gambar);
        finally
          gambar.Free;
        end;

        {
        Alternatively:

        gambar := zbiodata2.CreateBlobStream(zbiodata2.FieldByName('gambar'), bmWrite);
        try
          image1.Picture.Graphic.SaveToStream(gambar);
        finally
          gambar.Free;
        end;
        }
      end;

      zbiodata2.Post;    
    except
      zbiodata2.Cancel;
      raise;    
    end;
  except
    on E:Exception do
      ShowMessage('sorry this a problem .' + #13 + 'Error : ' + E.Message);
  end;
end;

If you are still having problems after that, you need to explain what is actually going wrong, what errors you are seeing, etc.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top