Ian Branch 127 Posted December 13, 2020 Hi Team, I have similar code to below in several places // pBlob := dmC.jtImages.CreateBlobStream(dmC.jtImages.FieldByName('JTImage'), bmWrite); // try pBlob.Seek(0, soFromBeginning); // fsFile := TFileStream.Create(sFileName, fmOpenRead or fmShareDenyWrite); try pBlob.CopyFrom(fsFile, fsFile.Size); finally fsFile.Free end; finally pBlob.Free end; // end else begin MessageBeep(MB_ICONERROR); TaskMessageDlg('File not found!', 'File ' + sFileName + ' not found!', mtError, [mbOK], 0); pBlob.Free; end; pBlob is declared as a TStream. I consistently get the following error message.. "[dcc32 Warning] XXXXXFrm.pas(1213): W1036 Variable 'pBlob' might not have been initialized" pointing to the last pBlob.Free; I know it is only a Warning but it is annoying in an otherwise clean compile/build. What am I missing? How do I rectify this please? Regards & TIA, Ian Share this post Link to post
Guest Posted December 14, 2020 (edited) dont economize words: what IDE used? --- Is top-secret? what component /class is "jtImages"? (is a TImage from another suite?) "dmC" is "DataModule do tipo C" 😄 etc... NOTE: depending of your IDE, "BLOB" fields (and others class, like TImage) have procedure to "LoadFromFile" and "SaveToFile", it's not necessary all this code! maybe we forget read your Help System: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/DB_TDataSet_CreateBlobStream.html http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/DB_TBlobType.htm { The following example copies the data in the Notes field of Table1 or SQLDataSet1 to the Remarks field of ClientDataSet1. } procedure TForm1.Button1Click(Sender: TObject); var Stream1: TBlobStream; Stream2: TStream; blobType : TBlobType; begin blobType := Table1Notes.BlobType; Stream1 := TBlobStream.Create(Table1Notes, bmRead); try CDS2.Edit; { here’s a different way to create a blob stream } Stream2 := CDS2.CreateBlobStream(CDS2.FieldByName('Remarks'), bmReadWrite); try Stream2.CopyFrom(Stream1, Stream1.Size); // CDS2.Post; // CDS2.Active := True; finally Stream2.Free; end; finally Stream1.Free; end; end; Edited December 14, 2020 by Guest Share this post Link to post
Ian Branch 127 Posted December 14, 2020 Hi emailx45, My apologies. I thought the code was fairly generic and therefore wouldn't need further clarification. My Bad. 😞 D10.4.1, ElevateDB. In this case jtImages is an ElevateDB Table Blob field. Yes, dmC is a datamodule. I am guilty of simple cut & paste when I incorporated my code. I note your alternative code. TBlobStream appears to be an element of the BDE, which I don't have installed. Ian Share this post Link to post
Ian Branch 127 Posted December 14, 2020 Actually I just discovered why I was getting the messages. I was doing a Free of pBlob at the end of the "if FielExists(sFileName) then ..." and of course nothing had happened with pBlob at that time. My Bad. Thanks for looking. Ian Share this post Link to post
Guest Posted December 14, 2020 (edited) ok! the BDE code is sample by Embarcadero, but the new fields blob FireDAC etc... can use loadfile/savefile more easy! remember: DONT FREE.FREE Edited December 14, 2020 by Guest Share this post Link to post
Ian Branch 127 Posted December 14, 2020 OK. My thanks to emailx45 for getting me on the right track & overcoming my laziness. 😉 My above code has been condensed to... // (dmC.jtImages.FieldByName('JTImage') as TBlobField).LoadFromFile(sFileName); // Thank you. Regards, Ian Share this post Link to post
Guest Posted December 14, 2020 (edited) 1 hour ago, Ian Branch said: My above code has been condensed to... good, but this way is more readable too: TBlobField(dmC.jtImages.FieldByName('JTImage')).LoadFromFile(sFileName); -- if YOU know it, then, is not necessary use "AS" operator! DONT FORGET: when working with obj is necessary "know" if it is "nil" or not... avoiding "AV" on the mid-way Edited December 14, 2020 by Guest Share this post Link to post
Ian Branch 127 Posted December 14, 2020 Hi amailx45, Interesting. That would not have occurred to me in a million years. Slight problem.. Delphi doesn't like it. :-( SFileName is an undeclared identifier... Ian Share this post Link to post
Ian Branch 127 Posted December 14, 2020 Interesting. I retyped the line and it, Delphi, is happy now. Share this post Link to post
Guest Posted December 14, 2020 (edited) when loading a file is interessant verify is "it" exist yet! then: try if fncFileExistYet( 'file do verify' ) then begin table.EDIT; TBloabField( xxxx ) loadfile ( xxxxx ); // if occurr any "AV" ... another fields actions table.POST; end; except on E:Exception do begin table.CANCEL; // any way if in "edit" or not! ShowMessage('my error ' + E.Message ); // show the message for last end; end; hug (I'll sleep .. 02:03hrs) Edited December 14, 2020 by Guest Share this post Link to post
Ian Branch 127 Posted December 14, 2020 Hi emailx45. Yes, I have those safeguards, including checking and limiting the image file type/size. Only .jpg or .jpeg & <256kB. Share this post Link to post