[imp] Active Directory Question Pls Reply

  • Thread starter Thread starter Arvind P Rangan
  • Start date Start date
A

Arvind P Rangan

Hi,

I working on Active Directorys, and am trying to retrive some values for
certain attribute.

No problem till now, but some attributes don't have values like
Title,Second Name,etc...

When i try to compare those values with isNothing or IsDbNull its still
raising System.NullReferenceException.

How to capture this before going to exception.
I need to check its nothing then display some message else the available
value.

Code
Dim res As SearchResult
Try

If res.Properties("title")(0) Is Nothing Then
Response.Write("No title")
Else
Response.Write(res.Properties("title")(0))
End If
Catch es As Exception
Response.Write(es.ToString)
End Try

Thanks
Regards
Arvind.
 
Arvind,

I would think that it would return null as well, but it might be
returning something else. Why not look in the watch window to see what a
known "null" value returns? Once you see it in your watch window, you can
code against it in your program.

Hope this helps.
 
Hi Arvind,

I don't know the structure of values in the Active Directory, but before I
go looking for it will you try first this.
If res.Properties("title")(0) Is Nothing Then
Means normaly it does not exist.

If res.Properties("title")(0)=Nothing
means that it exist but has the default value.

If this is an SQL or Access database it is
If res.Properties("title")(0)=dbnull.value

Will you try = nothing?

Cor
 
Unfortunately, there is no other way than catching the System.NullReferenceException when a property doesn't exists.
Change you code to something like this:
Try
Response.Write(res.Properties("title")(0))
Catch es As Exception
Response.Write("No title")
End Try

Willy.
 
Hi Cor, res.Properties("title")(0) is an object, and therefore cannot "=
Nothing". It can only be "Is Nothing"

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Willy Denoyette said:
Unfortunately, there is no other way than catching the System.NullReferenceException when a property doesn't exists.
Change you code to something like this:
Try
Response.Write(res.Properties("title")(0))
Catch es As Exception
Response.Write("No title")
End Try

Willy.

Another possibility is to check if the collection contains the named property, of course there is still a chance that the
PropertyValueCollection has no item in it, but this method is preferable over the previous :-)

If res.Contains("title") Is False Then
....

Willy.
 
Hi Arvind,

Arvind P Rangan said:
Hi,

I working on Active Directorys, and am trying to retrive some values for
certain attribute.

No problem till now, but some attributes don't have values like
Title,Second Name,etc...

When i try to compare those values with isNothing or IsDbNull its still
raising System.NullReferenceException.

How to capture this before going to exception.
I need to check its nothing then display some message else the available
value.

Code
Dim res As SearchResult
Try

If res.Properties("title")(0) Is Nothing Then
Response.Write("No title")
Else
Response.Write(res.Properties("title")(0))
End If
Catch es As Exception
Response.Write(es.ToString)
End Try

I haven't yet had a reason to use Directory Service, but a couple of
ideas come to mind.

1. Probably a dumb question, but are you sure that the variable
"res" has been properly initialized? I notice in your code snippet it is
not, but I'm assuming you omitted it.

2. The "Properties" property of the SearchResult class returns a
ResultPropertyCollection object. The ResultPropertyCollection class has a
"Contains" method which returns true if the collection contains a certain
property, otherwise false.

...
If res.Properties.Contains("title") Then
Response.Write(res.Properties("title")(0))
Else
Response.Write("No title")
End If
...

Regards,
Dan
 
When i try to compare those values with isNothing or IsDbNull its still
raising System.NullReferenceException.
How to capture this before going to exception.

Use the ".Count" property of the "Properties" property:

if (res.Properties["title"].Count > 0)
{
// do something
}

Or if that doesn't work, you can use the ".Contains" method of the
Properties:

if (res.Properties.Contains("title"))
{
// do something
}

One of the two should work, I think.

Marc
 
Back
Top