Recent

Author Topic: DBLookupCombobox can not convert null to int64 for data submission  (Read 950 times)

asimkon

  • Newbie
  • Posts: 2
I have been using Lazarus 2.x with Firebird 3 (via flamerobin) and i try to commit records into a table with a foreign key via TSQLConnection, TSQLQuery, TDataSource (data module).

I run the following script successfully in order to configure initially DBLookupCombobox (reference table) and the records are displayed without any problem from table1.

Code: Pascal  [Select][+][-]
  1. procedure TForm3.FormCreate(Sender: TObject);
  2.   begin
  3.   appeals.SQLQuery4.Close;
  4.   appeals.SQLQuery4.SQL.Text:='select id,fullname from promoter';
  5.  
  6.   appeals.SQLQuery4.Open;
  7.  
  8.   appeals.DataSource2.DataSet:=appeals.SQLQuery4;
  9.   DBLookupComboBox1.ListSource:=appeals.DataSource2;
  10.   DBLookupComboBox1.ScrollListDataset:=True;
  11.   DBLookupComboBox1.Style:=csDropDownList;
  12.   DBLookupComboBox1.KeyField:='id';
  13.   DBLookupComboBox1.DataField:='id';
  14.   DBLookupComboBox1.ListField:='fullname';    
  15.  
  16.   end;  

Afterwards i use the following script to submit all selected data to table2 with foreign key

Code: Pascal  [Select][+][-]
  1. procedure TForm3.Button1Click(Sender: TObject);
  2. begin
  3.  
  4.   appeals.SQLTransaction1.Active:=false;
  5.  
  6.   appeals.SQLQuery1.SQL.Text:='UPDATE appeals set name=:name,date_entry=:entry,date_suspended=:suspended,'+
  7.   'date_court=:court,date_judgement=:judgement,promoter_id=:code where id='+IntToStr(row_num);
  8.  
  9.   appeals.SQLQuery1.Params.ParamByName('name').AsString:=Trim(Edit1.Text);
  10.   appeals.SQLQuery1.Params.ParamByName('entry').AsDate:=DateTimePicker1.Date;
  11.   appeals.SQLQuery1.Params.ParamByName('suspended').AsDate:=IncDay(DateTimePicker1.Date,10);
  12.   appeals.SQLQuery1.Params.ParamByName('court').AsDate:=DateTimePicker2.Date;
  13.   appeals.SQLQuery1.Params.ParamByName('judgement').AsDate:=IncDay(DateTimePicker2.Date,20);
  14.   appeals.SQLQuery1.Params.ParamByName('code').AsInteger:=DBLookupComboBox1.KeyValue;
  15.  
  16.   appeals.SQLTransaction1.StartTransaction;
  17.   appeals.SQLQuery1.ExecSQL;
  18.   appeals.SQLTransaction1.Commit;
  19.   Application.MessageBox('Record submission with success !', 'Information', MB_ICONINFORMATION);                              
  20.  
  21.  end;

Unfortunately i receive the following error ( attached as image) that has to do with
Code: Pascal  [Select][+][-]
  1. appeals.SQLQuery1.Params.ParamByName('code').AsInteger:=DBLookupComboBox1.KeyValue;

I try to submit data via edit, datetimepicker and DBLookupCombobox as foreign key to a table (attached as image).

Any help would be convenient to me!

Regards

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: DBLookupCombobox can not convert null to int64 for data submission
« Reply #1 on: June 10, 2020, 12:16:01 pm »
At the top of the buttonclick you close the transaction of appeals.SQLQuery4. This will effectively close the dataset.
Because DBLookupComboBox1 is connected to that dataset (via ListSource), you also set the Keyvalue of DBLookupComboBox1 to null.

After that you try to assign a null to a asInteger (which isn't possible because null isn't a number).

I'm not sure what you are trying to do with the DBLookupComboBox but you could first save the code (or  DBLookupComboBox1.KeyValue) before closing the transaction. Then you can assign that saved value to asInteger.

But as I see it in the code... you don't want your code field to change, do you?
You UPDATE a record and want to assign 'code' field with the same value as the DBLookComboBox (which has the previous valie). So why don't you just remove de code assignment in the UPDATE statement ??

 

TinyPortal © 2005-2018