tcsh tcsh - 1 month ago 12
SQL Question

Modal buttons not working on form - delphi

I'm trying to save a .bmp from my PC to a database that contains a

blob
field.

I am currently working with code from here:

How to insert image into database using TADOQuery Component Only

For this purpose I've added the following in my app:


  • I use a
    TActionManager
    so I added a new action there, named Add Image - internal name is actAddImagine

  • Then I added the new action to a
    TPopUpActionBar
    .

  • I now go on my app, which contains a
    TDBGrid
    that displays the data from the database, via
    dataset
    .

  • My database contains various columns which hold various information (customer order details) and it also contains a column named pscreen which would hold an image if I load one there

  • I select an order (customer details and so on) from the list in my
    DBGRid
    , right click it and the
    PopupActionBar
    shows up with various options, including the Add Image I mentioned earlier, once I click this it opens up a new
    form
    .



This is the code for the action I mentioned earlier, named Add Image:

procedure TForm2.actAddImagineExecute(Sender: TObject);
begin
addImagineForm.Show;
end;


The form, as you can see, is named addImagineForm (which basically means Add Image, in my language)

This is how the form looks like:
enter image description here


  • It contains the following: 3
    TButton
    , 1
    TOpenDialog
    and 1
    TImage

  • Button named (as appears in the picture) Salveaza means Save (Button3)

  • Button named (as appears in the picture) Incarca Imagine means Load Image (Button1)

  • Button named Salveaza is set to Modal Ok

  • Button named Cancel (Button2) has Cancel property ticked and set to Modal Abort



The code for the Incarca Imagine button is this:

procedure TaddImagineForm.Button1Click(Sender: TObject);
var
selectedFile: string;
dlg: TOpenDialog;
begin
selectedFile := '';
dlg := TOpenDialog.Create(nil);
try
dlg.InitialDir := 'C:\';
dlg.Filter := 'All files (*.*)|*.*';
if dlg.Execute(Handle) then
selectedFile := dlg.FileName;
Image1.Picture.LoadFromFile(selectedFile);
finally
dlg.Free;
end;
end;


The code for button named Salveaza (Button3) is:

procedure TaddImagineForm.Button3Click(Sender: TObject);
var
Field: TBlobField;
Stream: TStream;
begin
if dbmodule.comenziQuery.Active and (Image1.Picture.Graphic <> nil) then
begin
dbmodule.comenziQuery.Insert;
Field := TBlobField(dbmodule.comenziQuery.FieldByName('pscreen')); // ensure it ís a blob
Stream := dbmodule.comenziQuery.CreateBlobStream(Field, bmWrite);
try
Image1.Picture.Graphic.SaveToStream(Stream);
finally
Stream.Free;
dbmodule.comenziQuery.Post;
end;
end;
end;



  • The
    TSQLQuery
    I'm using (named comenziQuery) is located on a
    Data module
    , along with other things I'm using (like
    DataSource
    ,
    DataSet
    ,
    SQLConnection
    and so on..) the Data module is named dbmodule

  • The column in the database that should hold the .bmp image is named pscreen and is set as a
    mediumblob
    type.



Problems:


  • When I click the
    Cancel
    button on the form, it does not close, basically does nothing.

  • When I click the
    Salveaza
    button on my form, it does nothing.



Things I've tried:


  • I've tried adding the
    close;
    and
    modalresult:=mrok;
    but it doesn't seem to execute the code for the image, just makes the Save button work and closes the form but no image added to the db field

  • Searched for tutorials on this, found a bunch but either they're not for what I need or I clearly don't understand how to overcome this and get it to work



I'm new to Delphi/SQL so I'm mostly working with tutorials from the net, answered question from the community here and various such resources, so clearly I must be doing a simple mistake somewhere due to me being new.

I've been at it for a couple of days now and cant seem to get this to work, to make those 2 buttons work (Cancel and Salveaza) and once I click the Salveaza button to run that code and see if it works.

I'd appreciate any insight, guidance you might have to share on this, perhaps you can notice something wrong I'm doing so far and can help me finish this functionality as it's giving me quite the headache, lol. Code examples using my already provided info would be more appreciated, if you have the time and desire, as it would speed things up and help to understand better and faster the whole process.

I'm using Rad Studio 10 Seattle, the database is
MYSQL
, the database components are located on a data module named dbmodule and contain the following:
TSimpleDataSet
,
TSQLQuery
,
TDataSource
,
TSQLConnection
- they are
dbExpress
components.

Sorry for the long post but I wanted to be as clear as possible in my explanation, hope I didn't leave anything out - leave a comment if so and I will edit.

Thanks! :)

Dsm Dsm
Answer

Your problem is the action

procedure TForm2.actAddImagineExecute(Sender: TObject);
begin
  addImagineForm.Show;
end;

which should be

procedure TForm2.actAddImagineExecute(Sender: TObject);
begin
  addImagineForm.ShowModal;
end;

for the modal buttons to have any meaning.

Edit

and additional problem is the lines

        if dlg.Execute(Handle) then
          selectedFile := dlg.FileName;
          Image1.Picture.LoadFromFile(selectedFile);

Your indentation suggests that Image1 will attempt to load only if dlg executes successfully. This is not the case. It will always attempt to load. Your code should be if dlg.Execute(Handle) then begin selectedFile := dlg.FileName; Image1.Picture.LoadFromFile(selectedFile); end;