Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 10-11-2016, 04:34 PM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 37
Default Use TOpenDialog or TOpenPictureDialog to load a .bmp and send it to a db blob field

Hi,

I have on my main form a DBGrid connected to a dataset.
The grid displays the table columns which number around 11.
These columns hold various order details for a new client I add

To add a client I'm using a form where I just fill out the information needed, it looks like this:



Notice I only add info for 9 of the 11 columns, the remaining 2 are ID column (auto increment) and a DATE column, they take care of themselves.

Ok, I've recently added a new column to my table, named pscreen (which stands for print screen)
I've set that to medium blob and I'm going to use it to store an image there, a .bmp

Naturally now I have to modify the above form so I can add info (the image) to that particular column too, when adding a new order.

I'd figure I'd add a new TButton on the form and a TOpenDialog.

Once I click the new TButton the TOpenDialog should fire up, I found the following code which I'm going to use for the button:

Code:
procedure TForm1.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;
  finally
    dlg.Free;
  end;

  if selectedFile <> '' then
    
end;
*no idea what to add in these last 2 lines of code here, or if I even need them

Right, so now the TOpenDialog pops up so I can use it once I click the TButton, dialog pops up, I double click the picture I want to add

and

this is where I'm stuck.

How do I sent this, along with the other info on the form, to the table?

Until now I used to fill in every TEdit with info I need and then click the TButton named 'Adauga' which you can see in the above picture, which basically means 'Add' - once I hit that the following code executes:

Code:
procedure TForm2.actAddComandaExecute(Sender: TObject);
begin
if dbmodule.comenziConnection.Connected then

begin
//clear fields
addOrderForm.Edit1.Text:='';
addOrderForm.Edit2.Text:='';
addOrderForm.Edit3.Text:='';
addOrderForm.Edit4.Text:='';
addOrderForm.Edit5.Text:='';
addOrderForm.Edit6.Text:='';
addOrderForm.Edit7.Text:='';
addOrderForm.Edit8.Text:='';
addOrderForm.Edit9.Text:='';
if addOrderForm.ShowModal=mrok then

begin
//here will implement sql
dbmodule.comenziQuery.SQL.Clear;
dbmodule.comenziQuery.SQL.Add( 'insert into `dbname`.`tablename` ( column names are here ) values ( :column names are here ) ' ) ;
dbmodule.comenziQuery.Params.ParamByName( 'column name is here' ).AsString := addOrderForm.Edit1.Text ;
dbmodule.comenziQuery.Params.ParamByName( 'column name is here' ).AsString := addOrderForm.Edit2.Text ;
dbmodule.comenziQuery.Params.ParamByName( 'column name is here' ).AsString := addOrderForm.Edit3.Text ;
dbmodule.comenziQuery.Params.ParamByName( 'column name is here' ).AsString := addOrderForm.Edit4.Text ;
dbmodule.comenziQuery.Params.ParamByName( 'column name is here' ).AsString := addOrderForm.Edit5.Text ;
dbmodule.comenziQuery.Params.ParamByName( 'column name is here' ).AsString := addOrderForm.Edit6.Text ;
dbmodule.comenziQuery.Params.ParamByName( 'column name is here' ).AsString := addOrderForm.Edit7.Text ;
dbmodule.comenziQuery.Params.ParamByName( 'column name is here' ).AsString := addOrderForm.Edit8.Text ;
dbmodule.comenziQuery.Params.ParamByName( 'column name is here' ).AsString := addOrderForm.Edit9.Text ;
dbmodule.comenziQuery.ExecSQL ;  //  record inserted

//we need to refresh the data
actRefreshData.Execute;
end;
I don't know what code to write and where to add it so I can also send the image I'm loading with TOpenDialog, along with the other information once I click the button named "Adauga".

The blob field is set to null so If I don't need to add an image to that order then I would just not edit that part (won't select an image with the dialog) and I'd just edit the other parts and hit Adauga, that should work as it did so far.

Can anyone help me out with this?

I realise it's a matter of sql to be added to the button named "Adauga" but I don't know how to write that for this thing, or even how to get that image I just loaded from the OpenDialog and specify it in the sql - bit too advance stuff for me at this point.
I have been reading about LoadFromStream but that got me completely stumped.
I've also seen the one with LoadFromFile but as I understand it that just loads the image from C:\img name without the need of a TOpenDialog, which doesn't help me.

Any help would be appreciated.
Thank you!
Please do let me know if I left anything out and/or you need further details/clarifications

Last edited by tcsh; 10-11-2016 at 04:37 PM.
  #2  
Old 10-12-2016, 08:00 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,335
Default

I guess you're looking for how to save (and probably load) a blob
http://stackoverflow.com/questions/1...component-only
  #3  
Old 10-12-2016, 08:11 AM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 37
Default

Quote:
Originally Posted by Norrit View Post
I guess you're looking for how to save (and probably load) a blob
http://stackoverflow.com/questions/1...component-only
If by load you mean retrieve the image from the table field and display it somewhere in my software then no, I got that covered.

I just need to open the image using my mouse with the TOpenDialog, and then send that to the field in my table, that's it.

I have no idea how to use those code examples in conjunction with a topendialog and my add button that I've explained above what it does.
  #4  
Old 10-12-2016, 09:32 AM
Jasser Jasser is offline
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,059
Default

Using the code in the link that Norrit's post:

You may need to add a TImage component and TADOQuery to your entry form then when you click the open dialog button your code should be something like this:


Code:
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;

And then when you close the entry form to save the image and the reset of the other fields your code for the image should be something like this:

Code:
var
          Field: TBlobField;
          Stream: TStream;
begin
          if ADOQuery1.Active and (Image1.Picture.Graphic <> nil) then begin
             ADOQuery1.Insert;
             Field := TBlobField(ADOQuery1.FieldByName('ImageData')); // ensure it ís a blob
             Stream := ADOQuery1.CreateBlobStream(Field, bmWrite);
             try
                Image1.Picture.Graphic.SaveToStream(Stream);
             finally
                Stream.Free;
                ADOQuery1.Post;
             end;
          end;
__________________
Regards,
Abdulaziz Jasser

Last edited by Jasser; 10-12-2016 at 10:13 AM.
  #5  
Old 10-12-2016, 10:29 PM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 37
Default

Thank for your reply, makes more sense now.

Unfortunately I'm getting stuck on adding the 2nd code, most likely due to what I'm using and not knowing where exactly to fit your code in.

Here is how this looks now:

This the form where I put in the order details (and the new picture)


This it's whole code

Code:
unit U_addOrderForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
  TaddOrderForm = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Label4: TLabel;
    Edit4: TEdit;
    Label5: TLabel;
    Edit5: TEdit;
    Label6: TLabel;
    Edit6: TEdit;
    Label7: TLabel;
    Edit7: TEdit;
    Label8: TLabel;
    Edit8: TEdit;
    Label9: TLabel;
    Edit9: TEdit;
    Button1: TButton;
    Button2: TButton;
    OpenDialog1: TOpenDialog;
    Button3: TButton;
    Image1: TImage;
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  addOrderForm: TaddOrderForm;

implementation

{$R *.dfm}

uses U_dbmodule;

procedure TaddOrderForm.Button2Click(Sender: TObject);
var gasit:boolean;

begin
// fields vaidation
gasit:=true;
if edit1.Text='' then gasit:=false; // not null fields
if edit2.Text='' then gasit:=false;
if edit3.Text='' then gasit:=false;
if edit4.Text='' then gasit:=false;
if edit5.Text='' then gasit:=false;
if edit6.Text='' then gasit:=false;
if edit7.Text='' then gasit:=false;
if edit8.Text='' then gasit:=false;
if edit9.Text='' then gasit:=false;

if gasit then
  begin
  close;
  modalresult:=mrok;
  end;


end;

procedure TaddOrderForm.Button3Click(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;

end.
When I press 'Adauga' button (which means Add) this is the current code that executes:

Code:
procedure TForm2.actAddComandaExecute(Sender: TObject);

begin
if dbmodule.comenziConnection.Connected then
begin
  //clear fields
  addOrderForm.Edit1.Text:='';
  addOrderForm.Edit2.Text:='';
  addOrderForm.Edit3.Text:='';
  addOrderForm.Edit4.Text:='';
  addOrderForm.Edit5.Text:='';
  addOrderForm.Edit6.Text:='';
  addOrderForm.Edit7.Text:='';
  addOrderForm.Edit8.Text:='';
  addOrderForm.Edit9.Text:='';
if addOrderForm.ShowModal=mrok then

begin
 //here will implement sql
 dbmodule.comenziQuery.SQL.Clear;
 dbmodule.comenziQuery.SQL.Add( 'insert into `tipotask`.`comenzi` ( stare, client, telefon, email, detalii, pret, livrare, user, status ) values ( :stare, :client, :telefon, :email, :detalii, :pret, :livrare, :user, :status ) ' ) ;
 dbmodule.comenziQuery.Params.ParamByName( 'stare' ).AsString := addOrderForm.Edit1.Text ;
 dbmodule.comenziQuery.Params.ParamByName( 'client' ).AsString := addOrderForm.Edit2.Text ;
 dbmodule.comenziQuery.Params.ParamByName( 'telefon' ).AsString := addOrderForm.Edit3.Text ;
 dbmodule.comenziQuery.Params.ParamByName( 'email' ).AsString := addOrderForm.Edit4.Text ;
 dbmodule.comenziQuery.Params.ParamByName( 'detalii' ).AsString := addOrderForm.Edit5.Text ;
 dbmodule.comenziQuery.Params.ParamByName( 'pret' ).AsString := addOrderForm.Edit6.Text ;
 dbmodule.comenziQuery.Params.ParamByName( 'livrare' ).AsString := addOrderForm.Edit7.Text ;
 dbmodule.comenziQuery.Params.ParamByName( 'user' ).AsString := addOrderForm.Edit8.Text ;
 dbmodule.comenziQuery.Params.ParamByName( 'status' ).AsString := addOrderForm.Edit9.Text ;
 dbmodule.comenziQuery.ExecSQL ;  //  record inserted

 dbmodule.arhivaQuery.SQL.Clear;
 dbmodule.arhivaQuery.SQL.Add( 'insert into `tipotask`.`arhiva` ( stare, client, telefon, email, detalii, pret, livrare, user, status ) values ( :stare, :client, :telefon, :email, :detalii, :pret, :livrare, :user, :status ) ' ) ;
 dbmodule.arhivaQuery.Params.ParamByName( 'stare' ).AsString := addOrderForm.Edit1.Text ;
 dbmodule.arhivaQuery.Params.ParamByName( 'client' ).AsString := addOrderForm.Edit2.Text ;
 dbmodule.arhivaQuery.Params.ParamByName( 'telefon' ).AsString := addOrderForm.Edit3.Text ;
 dbmodule.arhivaQuery.Params.ParamByName( 'email' ).AsString := addOrderForm.Edit4.Text ;
 dbmodule.arhivaQuery.Params.ParamByName( 'detalii' ).AsString := addOrderForm.Edit5.Text ;
 dbmodule.arhivaQuery.Params.ParamByName( 'pret' ).AsString := addOrderForm.Edit6.Text ;
 dbmodule.arhivaQuery.Params.ParamByName( 'livrare' ).AsString := addOrderForm.Edit7.Text ;
 dbmodule.arhivaQuery.Params.ParamByName( 'user' ).AsString := addOrderForm.Edit8.Text ;
 dbmodule.arhivaQuery.Params.ParamByName( 'status' ).AsString := addOrderForm.Edit9.Text ;
 dbmodule.arhivaQuery.ExecSQL ;  //  record inserted

 //we need to refresh the data
 actRefreshData.Execute;
end;
end;
end;
*Yes, I add the same information in 2 tables at once - this is ok.
*I won't add the image to the 2nd table (2nd block of sql shown here) just to the 1st table (1st block)

Assuming I did everything right up until this point, this is the 2nd code you pasted, modified to fit my names I use and so on:

Code:
var
          Field: TBlobField;
          Stream: TStream;

if dbmodule.comenziQuery.Active and (addOrderForm.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
addOrderForm.Image1.Picture.Graphic.SaveToStream(Stream);
finally
Stream.Free;
dbmodule.comenziQuery.Post;
end;
end;
* dbmodule is a data module named dbmodule. It contains all the SQLQuery, SQLConnection, TSimpleDataSet, TDataSource components on it and I use them from there (dbExpress)
* Notice I've changed your example code to fit my needs (replaces TADO as I'm not using it and so on)

Where exactly should I add this 2nd code, in to my own? I've tried mid / bottom / top / between - nothing works.
if you can provide a specific example, I'd appreciate it, thank you again!

Last edited by tcsh; 10-12-2016 at 10:33 PM.
  #6  
Old 10-16-2016, 07:19 AM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 37
Default

Can you help please? I can't figure out how/where to add that in my form - see above post
  #7  
Old 11-01-2016, 10:25 AM
tcsh tcsh is offline
Member
 
Join Date: Sep 2016
Posts: 37
Default

Apparently that code uses an Insert, which adds a new record, doesn't update a current one - which is what I need.

Marking this as Solved since I eventually found the answer, and thanks for the member who pm'ed me with some code example


Have a good one.

Last edited by tcsh; 11-13-2016 at 07:57 PM.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 08:26 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.