Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Empty User Profile Property throws exception

  Asked By: Seth    Date: Mar 05    Category: Sharepoint    Views: 2766

I'm trying to check a custom property to ensure it has data before
continuing any more procedures. Here is a portion of the code:

If pManager.UserExists(currentUser.LoginName) Then

Dim uProfile As UserProfile =
pManager.GetUserProfile(currentUser.LoginName.ToString())

'

' Check to make sure the Custom1 and Custom2 values are the
right length. If not then somethings wrong.

'

Try

If uProfile("Custom1").Value.ToString() Is Nothing Then

strError = "Invalid Custom1 or Custom2 ID. Please
contact the Service desk for help"

Return False

Else

strCustom1 = uProfile("Custom1").ToString()

strCustom2 = (uProfile("Custom2").ToString())

Return True

End If

Catch ex As Exception

strError = ex.Message & " : GetUserDetails() : " &
ex.Source

End Try

When I have data in the custom field the function runs fine. However, when I
delete the data in the custom fields to test error scenarios I get a "Object
reference not set to an instance of an object". I've tried
IsDBNull(uProfile("Custom1").Value.ToString()) and System.DBNull.Value. But
no matter what I use to check it always throws the "Object blah blah"
exception.

Does anyone know how to properly check for null or empty profile property
values?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Girja Garg     Answered On: Mar 05

It seems to me that your problem is here:

If uProfile("Custom1" ).Value.ToString () Is Nothing Then

What .NET is doing is attempting to convert the value to a stirng. However, if
the value is null/nothing then an exception  is thrown.

My suggestion is to try String.IsNullOrEmpty(uProfile("Custom1").Value)
instead.

 
Didn't find what you were looking for? Find more on Empty User Profile Property throws exception Or get search suggestion and latest updates.




Tagged: