To add a new record to a table, you need to perform the following actions:
// clear the record buffer
ARecord.Clear();
// populate the record buffer
ARecord.SetFieldAsSigned(0, 1000);
ARecord.SetFieldAsString(1, "Joe Smith");
// write the record
try
{
ARecord.Write();
}
catch (CTException err)
{
Console.Write("Add record failed with error {0}\n", err.GetErrorCode());
}
A record is added to a table if the new record flag and edited flag are both set. CTRecord.Clear() sets the new record flag, while one of the CTRecord.SetFieldAs...() functions will set the edited record flag.
If you clear a record and then, without populating the record buffer with data, make a call to CTRecord.Write(), no error code will be returned but no data record will be written to disk. This is because new record flag is set but the edited record flag is cleared.
If you want to write a cleared or blank record to disk, you must clear the record buffer, set the edited record flag by calling ctdbSetEditedRecord(), and then write the record by calling CTRecord.Write().
// clear the record buffer
ARecord.Clear();
// set the edited record flag
ARecord.SetEdited(true);
// write the record
try
{
ARecord.Write();
}
catch (CTException err)
{
Console.Write("Write record failed with error {0}\n", err.GetErrorCode());
}