Developer forum

Forum » Development » How to handle nullable user custom fields

How to handle nullable user custom fields

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

When I add a new Checkbox (boolean) field to a user in the control panel, it's created as a nullable bit in the database.

However, when saving that field for the user, it's actually required to provide a value; we have to set it to true or false or we get an error when saving the user through the API. For the backend this doesn't seem to be an issue as it assumes "false" for an unchecked checkbox. It turns out that we can set it to DBNull.Value as well which is then stored as a null in the database.

Is this a bug? Should it work with proper .NET nulls instead? It feels that using DBNull.Value is a little close to the database and a bit counterintuitive.

Thanks!

Imar

 


Replies

 
Nicolai Pedersen
Reply

Hi Imar

In this case null and false is handled in the same way. Or basically anything not true. Changing to a nullable boolean could cause compatibility to break, so I would rather not. And why would you ever want a null on the bit - it cannot be seen in the UI anyways, and would probably never be handled as a null anywhere, so maybe a list of something would be a better option to make it more explicit that the value can be set or not.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Nicolai,

I am not requesting a change, I think. I am OK with False and null being the same thing. It's just that we're seeing code crash with scenarios like this:

1. I add a field called IsBoolean to the backend for the user (type checkbox)

2. Some custom code updates the user:

user = User.GetUserByID(123);
var testField = user.CustomFieldValues.First(x => x.CustomField.SystemName == "AccessUser_SomeNonBoolField");
testField.Value = "Test";
user.Save();

This code crashes because it tries to send null for the new IsBoolean field. I could set it to false from code, but it's somewhat hard to discover that I need to do that because I am setting a completely unrelated field.

Does that make sense? 

Imar

 

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Ah, now I understand

Changed it, TFS#40405, sets dbnull for values of nothing on bit fields.

 

NP

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Thanks, that looks great!

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

On second thought: shouldn't it do this for other field types as well? DateTimes for example suffer from the same issue...

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Done!

Votes for this answer: 2
 
Rui Silva
Reply

Hi,

I've a solution recently upgraded to 8.9.2.14 that started to give an error when saving a new user:

Cannot set Column 'AccessUser_DefaultExpDate' to be null. Please use DBNull instead

The field is a custom field for the solution and with the previous version (8.8.1.13) no error has generated when saving the new user.

Can this be the same issue?

BR,

Rui Silva

 
Nicolai Pedersen
Reply

Hi Rui

I've send this to QA for verification and re-opening if it is the same issue.

 
Oleg Rodionov Dynamicweb Employee
Oleg Rodionov
Reply

Hi Rui,

I've checked my test environment based on DW8.9.2.14 with regular way to add new user and was not able to catch an exception, I use user custom field of all available types - https://www.screencast.com/t/CTdNAuuf91

Besides, I've tried to downgrade my solution to 8.9.2.13, add new custom field type of Date/DateTime, move back to 8.9.2.14 and perform the adding again - the same result is. 

So, please, comment if you use other way and conditions are different against mine.

BR, Oleg QA

 
Rui Silva
Reply

I’ve check it deeper and the exception I’ve mentioned is throwed when my code calls the Save method from the API for a new Group object (not a User sorry).

I mentioned the user custom field because the field name is from the AccessUser table, and since both types of objects shared the same table that lead me to the misdirection.

 

 
Oleg Rodionov Dynamicweb Employee
Oleg Rodionov
Reply

Hi Rui,

I was able to reproduce the bug on DW892 on creating new user during checkout. So, new separate TFS42060 has been created to fix 892, will be provided with further hotfix soon. Thanks.

BR, Oleg QA 

 
Kristian Kirkholt Dynamicweb Employee
Kristian Kirkholt
Reply

Hi

The problem TFS#42060 "Error exception on user creating in checkout on DW89" has now been fixed in version 8.9.2.15

You are able to find this build in the download section:

http://doc.dynamicweb.com/releases-and-downloads/releases

Please contact Dynamicweb Support if you need any additional help regarding this.

Kind Regards
Dynamicweb Support
Kristian Kirkholt

 

You must be logged in to post in the forum