Exception thrown creating new record for custom table

Don Rea asked on April 7, 2022 17:07

We created a custom table through the admin interface following the instructions at https://docs.xperience.io/k11/developing-websites/defining-website-content-structure/custom-tables/creating-custom-tables Now we are beginning to test some code that interacts with the table via the DataClassProvider. This is the code that tries to insert a new row, and as you can see except for changing the variable identifiers it is copied exactly from https://docs.xperience.io/api11/content-management/custom-table-data#Customtabledata-Addingdatarecordstoacustomtable:

string nonceClassName = "customtable.Nonce";
DataClassInfo nonceTable = DataClassInfoProvider.GetDataClassInfo(nonceClassName);
if (null != nonceTable)
{
    var newNonce = CustomTableItem.New(nonceClassName);
    newNonce.SetValue("NonceID", nonceId);
    newNonce.SetValue("NonceValue", nonceValue);
    nonceTable.Insert();
}

But when execution reaches this block IIS emits the following Exception information:

[CodeNameNotUniqueException: The custom table with code name 'customtable.Nonce' already exists.]
   CMS.DataEngine.AbstractInfoProvider`3.SetInfo(TInfo info) +871
   CMS.DataEngine.DataClassInfoProvider.SetInfo(DataClassInfo info) +347
   PaTTAN.Auth.OpenIdHelper.CreateNonce(Int32 byteCount) +433
...

Everything I've been able to find about CodeNameNotUniqueException describes situations that do not seem to apply here, mostly having to do with object caching on repeated calls. Can anyone suggest what we ought to be doing differently? I'd hate to have to write to the DB outside the API, but I do need to get this working fairly soon.

Recent Answers


Juraj Ondrus answered on April 8, 2022 06:51

I just tested the API code and works fine. Where and how are you running the code? What does debugging the code says? Isn't it called multiple times?

0 votesVote for this answer Mark as a Correct answer

vasu yerramsetti answered on April 11, 2022 05:06

I have replicated this in my local.

Problem with the following line of code because it is not required and it is generated by Kentico as Identity filed and it is not allowed to duplicate value.

newNonce.SetValue("NonceID", nonceId);

Please remove this line and try. It should work as per your expectations.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.