How check if property is empty with contentService in Umbraco 7.1.1?
Hi everyone,
I get a System.NullReferenceException (Object reference not set to an instance of an object) if I will get a value from a empty property (Textbox multiple) with a user control for frontend. How can I check if a property is empty?
This is the part of code with contentService:
var contentService = ApplicationContext.Current.Services.ContentService; var myNode= contentService.GetById(1437);
I have found the next curious thing now. If I get the exception from above and make nothing and wait about 30 minutes and refresh the page in frontend again suddenly it works again. Then if I publish the content node and refresh frontend I get the exception.......
I know not if you have found a solid solution after a few years or three :) for this but according to the documentation an approach would be the following:
var contentService = ApplicationContext.Current.Services.ContentService;
var myNode= contentService.GetById(1437);
var bodyText= myNode.GetValue<string>("content");
if(bodyText != null) {
@bodyText
}
The reason you cannot do a myNode.GetValue("content").ToString() is that it can be empty, thus at runtime before it know it tries to bind to something empty, so you have to check for "null" before you return a string OR get the value as shown here.
If anybody else has other ideas it would be great to hear some.
How check if property is empty with contentService in Umbraco 7.1.1?
Hi everyone,
I get a System.NullReferenceException (Object reference not set to an instance of an object) if I will get a value from a empty property (Textbox multiple) with a user control for frontend. How can I check if a property is empty?
This is the part of code with contentService:
This works if the property is not empty:
If I copy the dll from user control in web folder and call the page in frontend without republishing the content node this works great:
But if I republish now the content node in umbraco and refresh the page in frontend again I get the System.NullReferenceException.
This is the same issue everytime. Copy the dll in web folder and it works again. But if I republish the content node I get the Exception again.
How can I solve this?
Best regards
Sören
I have found the next curious thing now. If I get the exception from above and make nothing and wait about 30 minutes and refresh the page in frontend again suddenly it works again. Then if I publish the content node and refresh frontend I get the exception.......
Did you find a solution for this? I¨m getting System.NullReferenceException on this
if (content.HasProperty("myProperty") && !String.IsNullOrEmpty(content.GetValue("myProperty").ToString()))
If the property has a value, it works fine. It also worked fine with Umbraco 6.
Hi Arild,
this works fine for me in Umbraco 7.1.7. If you have an older version of Umbraco 7 you should be an update to the latest release.
Is "myProperty" the alias of your property or the name? You must use the alias.
This works for me in an eventhandler:
Sören
Hi Sören
I know not if you have found a solid solution after a few years or three :) for this but according to the documentation an approach would be the following:
The reason you cannot do a myNode.GetValue("content").ToString() is that it can be empty, thus at runtime before it know it tries to bind to something empty, so you have to check for "null" before you return a string OR get the value as shown here.
If anybody else has other ideas it would be great to hear some.
is working on a reply...