String.IsNullOrWhitespace isn't Returning True for System.DBNull .. How to Check for DBNull in Macro

kentico guy asked on July 29, 2020 20:06

I have a macro expression that is as follows in K12 Portal Engine:

if (!String.IsNullOrWhitespace(MyPageTypeString)) 
{

  if (MyPageTypeString.Trim().Contains("xxxx.xxxx.net")) { //do something }

}

and I'm getting the following exception:

Method 'Trim' for object of type 'System.DBNull' not found, please check the macro or the method declaration.

So I'm wondering how I'm supposed to check for a System.DBNull object? I don't see any typeof() method in the macro methods. Isn't the string.IsNullOrWhiteSpace() supposed to return true for a System.DBNull?

Correct Answer

kentico guy answered on July 31, 2020 16:42

The line you highlighted was working fine. The issue was with String.IsNullOrWhitespace() returning true even when the object was System.DBNull. There was no MyPageTypeString in that particular page but all the sudden that isnull method started returning true.

I never did figure out why; what I ended up having to do was stop using string.IsNullOrWhitespace(MyPageTypeString) and instead manually do this

if (CurrentDocument.GetValue("MyPageTypeString", "").ToString("") != "") { //run conditional code }

I have yet to figure out why this happened. I didn't change anything else in the app. No idea why I had to change it like this, but it's working now

1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on July 30, 2020 18:35 (last edited on July 30, 2020 18:35)

I believe your expression is invalid. You need to use the proper macro syntax.

if (!String.IsNullOrWhitespace(MyPageTypeString)) 
{

  if (Contains(Trim(MyPageTypeString), "xxxx.xxxx.net")) { //do something }

}

You can test your macro as well under System > Macros > Console

0 votesVote for this answer Mark as a Correct answer

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