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
w: http://www.ceosyd.catholic.edu.au/
 
_______________________________________________ ozmoss mailing list 
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