RemoteDB inserting records with Identity field

I am using the following code to insert new records in a table that has an Identity key field defined as [Id] [int] IDENTITY(1,1) NOT NULL
When inserting I get the message 'Field 'Id' must have a value'
As field Id is autoupdated when the record is created, how is this done using RemoteDB.

var
  qry: TXDataset;
  sSql: String;

begin
  sSql := 'SELECT * FROM LogData WHERE 1=0;';
  qry := TXDataset.Create(nil);
  qry.KeyFields := 'Id'
  qry.AutoApply := True;
  qry.Database := dbRemoteDB;
  qry.SQL.Text := sSql;
  qry.Open;

  qry.Insert;
  qry.FieldByName('Date').AsDateTime := Now;
  qry.FieldByName('PC').AsString := GNComputerName();
  qry.FieldByName('User').AsString := sUser;
  qry.FieldByName('Tipo').AsString := sType;
  qry.FieldByName('UserId').AsInteger := lUserId;
  qry.Post;
end;

You can set the Required property of the Id field to False.

As far as I know, in MS SQL Server identity columns do not have a Required property, as implicitly allways automatically gets a new value as new records are created.
Do you know a hack to do what you are proposing.

Not sure what do you mean by a hack? Did the proposed solution work?

It has been difficult to understand what you meant by the Required property, since I thought it was about modifying the field in the database, but now I have seen that it was about changing the property of this field in the recordset.
Once done, now it is working.
Thanks for your help.

qry.FieldByName('Id').Required := False;
1 Like

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