Id not set on entry of class

there is a table : SysCode
there is no data in SysCode initially

if (not CheckIfExistedInSysCode('MachNameMapTob','*',0)) then
begin
InsertSysCodeKind('MachNameMapTob','Map Data');
end;

var existed := CheckIfExistedInSysCode('MachNameMapTob','*',0);
if(existed) then
  ShowMessage('@@ * EXISTED!!')
else
  ShowMessage('* NOt existed !!');

first ,i run the program
there is no data existed then it will 'InsertSysCodeKind'
there has data inside SysCode
but when run ' var existed := CheckIfExistedInSysCode('MachNameMapTob','*',0);'
i will get error
image

then i must stop the program
but if i run the program again
there has data inside SysCode
then it run ' var existed := CheckIfExistedInSysCode('MachNameMapTob','*',0);'
no error shown
why ?

You are trying to insert an Integer id with value 0. That is a "no value" for Aurelius. If you really want to allow 0 as a valid id value, then use IdUnsavedValue to indicate a value that will mean "no id":

'CodeInx' (SMALLINT) and NotShow(BIT)
ther are 'not' 'Id'
they are just normal columns(fileds) but as part of primay key
CodeInx can have value as '0'

CREATE TABLE "SysCode" (
"CodeType" VARCHAR(20) NOT NULL DEFAULT '' ,
"CodeTypNM" VARCHAR(30) NULL DEFAULT '' ,
"CodeInx" SMALLINT NOT NULL DEFAULT 0,
"CodeNo" VARCHAR(4) NOT NULL DEFAULT '' ,
"CodeNM" VARCHAR(40) NULL DEFAULT '' ,
"CodeDesc" VARCHAR(255) NULL DEFAULT '' ,
"NotShow" BIT NOT NULL DEFAULT 0,
"CreateDate" DATETIME NOT NULL DEFAULT 'getdate()',
"CreateUser" VARCHAR(10) NOT NULL DEFAULT '' ,
"UpdateDate" DATETIME NULL DEFAULT NULL,
"UpdateUser" VARCHAR(10) NULL DEFAULT '' ,
PRIMARY KEY ("CodeInx", "CodeNo", "CodeType")
)
;

you can find in the "CREATE TABLE" SCRIPT

image
you will find the data been inserted although CodeCodeInx = 0

in reality, there are some data which has CodeCodeInx = 0

i add 'Id...'
then i got error

Ok, I see now that the problem in your code is that SysCode object is destroyed. If you run everything a second time, the object is retrieved. The reason is that you are destroying the object after saving, and you should not do it, because Aurelius manager will destroy it already:

    try
      with sysCode do
      begin
        CodeType := inCodeType;
        CodeTypNM := inCodeTypNM;
        CodeInx := 0;
        CodeNo := '*';
        CodeNM := '';
        CodeDesc := '';
        NotShow := 0;
        CreateDate := Now();
        CreateUser := 'cova';
        UpdateDate := CreateDate;
        UpdateUser := CreateUser;
      end;
      Form1.arlsmngr.Save(sysCode);
      Result := True;
      message := 'SUCCESS';
    finally
      // DON'T DO THIS
//      FreeAndNil(sysCode);
    end;

A better approach is doing it this way:

    var sysCode := TSysCode.Create;
    Form1.arlsmngr.ObjManager.AddOwnership(sysCode);
    var message := 'fail';
    with sysCode do
    begin
      CodeType := inCodeType;
      CodeTypNM := inCodeTypNM;
      CodeInx := 0;
      CodeNo := '*';
      CodeNM := '';
      CodeDesc := '';
      NotShow := 0;
      CreateDate := Now();
      CreateUser := 'cova';
      UpdateDate := CreateDate;
      UpdateUser := CreateUser;
    end;
    Form1.arlsmngr.Save(sysCode);

All that process is explained in details in the manual. Please check:

https://doc.tmssoftware.com/biz/aurelius/guide/objects.html#memory-management

Problems Solved !
TKS

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.