2010-04-14 74 views
1

我有一個用戶配置文件屬性。用戶未分配任何值的屬性。如果我使用下面的代碼。據thorwing例外「對象引用不設置到對象的實例」空的sharepoint用戶配置文件屬性拋出異常

userprof["OptOut"].ToString() 

我嘗試了所有類型的 像

if (userprof["OptOut"] != null) 
      OR 
if(userprof["OptOut"].Value != null) 

沒有摸索出適合我。

此處的userProf對象具有值。 userprof [「OptOut」]。Value is null

如何處理?

回答

0

您必須檢查它像這樣:

if (userprof["OptOut"][0] != null) 

我用這個方法:

private string GetPropertyValue(UserProfile userProfile, string propertyName) 
    { 
     try 
     { 
      if (userProfile[propertyName][0] == null) 
       { 
       //code like this: 
       //return "'" + propertyName + "' value is null"; 
       } 

      return userProfile[propertyName].ToString(); 
     } 
     catch (Exception ex) 
     { 
     //code like this: 
     //return string.Format("Error with '{0}' UP-property: {1}", propertyName, ex.Message); 
     } 
     return "-"; 
    } 
相關問題