Exception error if there is no associated data

Hello Gyse,
i have an database table "employee" and associated table "servicetime".
If I've added a new employee to the database but haven't yet recorded any service hours for that employee, I get an exception when I view the employee.
As soon as I have insert service hours for the employee, I don't get an exception anymore.
What am I doing wrong? The code is "very identical" to the example in the Aurelius manual page 208.

a short code-snip:

employees:=FManager.Find<Tapp_Employee>.OrderBy('emp_lastname').List;
if employees.Count>0 then
try
adsEmployee.SetSourceList(employees);
adsEmployee.Open;
if (not adsEmployee.FieldByName('emp_servicetime').IsNull) then
begin
try
adsServicetime.DatasetField:=(adsEmployee.FieldByName('emp_servicetime') as TDatasetField); // <-- here I get the exception. If no service hours have been recorded for the employee
adsServicetime.Open;
sgServicetime.Visible:=(adsServicetime.RecordCount>0);
finally
end;
end;
....

What is the exception text?
Do you have adsServicetime.Close before this ?
Could you paste the 2 entities type definition?

1 Like

In addition to what @Ferrari_Julio said, could you also provide the call stack at the moment of the error, and provide the persistent fields defined in the datasets - or are they created dynamically?

Hello Julio,
the exception Text:


Sorry but it's german delphi version ;)

Here the servicetime entity:
[Entity, Automapping]
[Table('app_servicetime')]
Tapp_servicetime = class
private
Fid: Integer;
Fst_day: Nullable;
Fst_time_from: Nullable;
Fst_time_to: Nullable;
function GetDuration: TTime;
function GetTimeFromText: string;
function GetTimeToText: string;
function GetDurationText: string;
function GetWeekdayText: string;
public
property id: Integer read Fid write Fid;
property st_day: Nullable read Fst_day write Fst_day;
property st_time_from: Nullable read Fst_time_from write Fst_time_from;
property st_time_to: Nullable read Fst_time_to write Fst_time_to;
public
property st_duration: TTime read GetDuration;
property st_timefromString: string read GetTimeFromText;
property st_timetoString: string read GetTimeToText;
property st_durationString: string read GetDurationText;
property st_weekday: string read GetWeekdayText;
end;

and here the employee entity:
[Entity,Automapping]
[Table('app_employees')]
Tapp_employee = class
strict private
Fid: Integer;
Femp_firstname: string;
Femp_lastname: string;
Femp_street: Nullable;
Femp_zipcode: Nullable;
Femp_city: Nullable;
Femp_phone: Nullable;
Femp_mobile: Nullable;
Femp_mail: Nullable;
Femp_entry: Nullable;
Femp_exit: Nullable;
Femp_isDriver: Nullable;
Femp_color: Nullable;
[ManyValuedAssociation([TAssociationProp.Lazy],CascadeTypeAll)]
Femp_servicetime: Proxy<TList<Tapp_servicetime>>;
[ManyValuedAssociation([TAssociationProp.Lazy],CascadeTypeAll)]
Femp_servicefree: Proxy<TList<Tapp_servicefree>>;
function Getservicetime: TList<Tapp_servicetime>;
function Getservicefree: TList<Tapp_servicefree>;
function GetFullName: string;
public
constructor create; overload;
destructor Destroy; overload;
property id: integer read Fid write Fid;
property emp_firstname: string read Femp_firstname write Femp_firstname;
property emp_lastname: string read Femp_lastname write Femp_lastname;
property emp_street: Nullable read Femp_street write Femp_street;
property emp_zipcode: Nullable read Femp_zipcode write Femp_zipcode;
property emp_city: Nullable read Femp_city write Femp_city;
property emp_phone: Nullable read Femp_phone write Femp_phone;
property emp_mobile: Nullable read Femp_mobile write Femp_mobile;
property emp_mail: Nullable read Femp_mail write Femp_mail;
property emp_entry: Nullable read Femp_entry write Femp_entry;
property emp_exit: Nullable read Femp_exit write Femp_exit;
property emp_is_driver: Nullable read Femp_isDriver write Femp_isDriver;
property emp_color: Nullable read Femp_color write Femp_color;
property emp_servicetime: TList<Tapp_servicetime> read GetServicetime;
property emp_servicefree: TList<Tapp_servicefree> read GetServicefree;
public
property emp_fullname: string read GetFullName;
end;

What is really interesting is that the error ONLY appears when no records are stored in "servicetime". As soon as I insert a record, everything is okay.

Could you please paste it again and use the Preformatted text feature ( the </> toolbar button ) ?
All your Nullable type declarations went invisible, as they were interpreted as HTML tags.
Please paste Create and Destroy procedures.
And don't forget what @wlandgraf asked:

could you also provide the call stack at the moment of the error, and provide the persistent fields defined in the datasets - or are they created dynamically?

(Noticed you're using overload instead of override with Create and Destroy)

1 Like

Do you initialize Femp_servicetime under Tapp_employee.create?

Yes.
constructor Tapp_employee.create;
begin
inherited;
Femp_servicetime.SetInitialValue(TList<Tapp_servicetime>.Create);
Femp_servicefree.SetInitialValue(TList<Tapp_servicefree>.Create);
end;

Also please fix this, you should be using override in Destroy. and no directive in Create.