I am trying to get information from a lookup field. I know SharePoint adds additional characters when this is done, and that is where my challenge is...I am trying to parse out the characters, and this is working when there is data in the field. However if the field is blank, I get a NullReferenceException. This is what my snippet looks like...
string test = (DefaultEmpty(myItem, "Lookup field"));
private static string DefaultEmpty(SPListItem item, string field)
{
return item[field] == null ? string.Empty : item[field].ToString();
}
private string ParseLookupField(string col)
{
String strColUnit = string.Empty;
if (string.IsNullOrEmpty(col) == false)
{
SPFieldLookupValue myValue = new SPFieldLookupValue(col);
strColUnit = myValue.LookupValue.ToString();
} return strColUnit;
}when the lookup field is blank and I step through the code, I get characters like "137;#" passed into my block that I am using to parse the data. Each time this happens, I get the NullReferenceException when the value is being passed to my 'strColUnit' variable. Is there a way to get this code to handle this exception? Thanks.