Thanks for all the different methods!

I’ve had no luck with the non-coalescing or ternary operators. Checking for 
null value simply doesn’t seem to cut it.

Finally cracked it with Salaman’s example.

string iTitle = "Untitled";
if (listItem["Title"] != null && 
SPEncode.HtmlEncode(listItem["Title"].ToString()) != string.Empty && 
!string.IsNullOrEmpty(listItem["Title"].ToString()))
{
iTitle = listItem["Title"].ToString();
}

Is it just me that goes through all this pain trying to achieve the most basic 
needs?

From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of 
Shahram Banihashem
Sent: Wednesday, 19 October 2011 10:40 PM
To: ozMOSS
Subject: Re: Check null or empty on it field

This is the way I go:

country = (item["Country"] ?? "No Country").ToString();

All in one line, neat and easy!

Regards,

Shahram.

________________________________
From: Salman Ahmad <salman.ah...@msn.com>
To: ozmoss@ozmoss.com
Sent: Wednesday, 19 October 2011 10:21 PM
Subject: RE: Check null or empty on it field

It should work,

if (item["Country"] != null )
{
country = item["Country"].ToString();
}

You can also check this way,

if (item["Country"] != null && SPEncode.HtmlEncode(item["Country"].ToString()) 
!= string.Empty && !string.IsNullOrEmpty(item["Country"].ToString()))
{
country = item["Country"].ToString();
}

 Regards,
Salman Ahmad

________________________________
From: paul.no...@ceosyd.catholic.edu.au
To: ozmoss@ozmoss.com
Date: Wed, 19 Oct 2011 12:44:26 +1100
Subject: RE: Check null or empty on it field
That looks great Nigel thanks.

And the null-coalescing operator checks both null and empty?


From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of 
Nigel Witherdin
Sent: Wednesday, 19 October 2011 12:42 PM
To: OzMoss
Subject: RE: Check null or empty on it field

Rather than pull the title out to a seperate string variable and then check on 
it, I would use something like:

string iTitle = (string)(spListItem["Title"] ?? "No Title");
string iDesc = (string)(spListItem["Description"] ?? "No description");

I believe casting the field value to string is more efficient then using a 
ToString function, and using the null-coalescing operator is more efficient 
then a seperate "if" statement (though I am prepared to be shouted down over 
this :)
Also, I think it is a lot neater in the code, and the same form can be used for 
different datatypes:

DateTime someDate = (DateTime)(item["Date Field Name"] ?? DateTime.MinValue);

Cheers,

Nigel

________________________________
From: paul.no...@ceosyd.catholic.edu.au
To: ozmoss@ozmoss.com
Date: Wed, 19 Oct 2011 11:10:58 +1100
Subject: Check null or empty on it field
Hi all,
I’m trying to put some checks in place to avoid exceptions when a field has no 
value. I had tried null but it didn’t seem to be enough. I’m now trying the 
following but continue to get an exception when a field in any item is empty.
Is there a more robust way to do this? Check length as well, or instead??
// Check the value for each string and supply a defualt if none exists
if (string.IsNullOrEmpty(iTitle))
{
    iTitle = "No Title";
}
if (string.IsNullOrEmpty(iDesc))
{
    iDesc = "No description";
}

Kind regards,

Paul Noone

---------------------------------------------------
Online Developer/SharePoint Administrator
Infrastructure Team, ICT
Catholic Education Office, Sydney
p: (02) 9568 8461
f: (02) 9568 8483
e: paul.no...@ceosyd.catholic.edu.au<mailto:paul.no...@ceosyd.catholic.edu.au>
w: http://www.ceosyd.catholic.edu.au/


_______________________________________________ ozmoss mailing list 
ozmoss@ozmoss.com<mailto:ozmoss@ozmoss.com> 
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss

_______________________________________________ ozmoss mailing list 
ozmoss@ozmoss.com<mailto:ozmoss@ozmoss.com> 
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss

_______________________________________________
ozmoss mailing list
ozmoss@ozmoss.com<mailto:ozmoss@ozmoss.com>
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss

_______________________________________________
ozmoss mailing list
ozmoss@ozmoss.com
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss

Reply via email to