Monday, March 26, 2012

Object reference not set to an instance of an object

here is my code.. i am trying to get the zipcode information from an active directory server

<%
Dim entry As New DirectoryServices.DirectoryEntry("LDAP://schpdc")
Dim Dsearch As New DirectoryServices.DirectorySearcher(entry)
Dim zip As String
Dim UserName As String
Dim testname As String
zip = "error"
UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split("\")(1)
Response.Write(UserName & "<br>")
Dsearch.Filter = "(cn=" + UserName + ")"
Response.Write(Dsearch.Filter)
Try
zip = Dsearch.FindOne.Properties("zipcode").ToString()
Catch E As Exception
zip = "Error: " & E.Message
End Try
Response.Write(zip)
%>

please help.. i am now just spinning my wheels!!!

Which line is throwing the error?


To be honest i am not sure what is causing the error.. however i am pretty sure it is in the try statement as that is what actually writes it out and it always runs the catch. (sorry for the delay in reply.. i was sick yesterday)


Put breakPoint to debud the error. Usually, the error happened in Name.Split("\")(1) or Properties("zipcode").ToString(). Because you already put try.. catch for Properties("zipcode").ToString(), I am afraid the error was caused by Name.Split("\")(1).


i dont think that is the case.. i added a line to make the username = to the full return (instead of the split return) here is what i get

FORTITECH\intranet
intranet
(& (objectClass=user)(cn=intranet))Error: Object reference not set to an instance of an object.

the first line is the return unsplit user name... the second is the split user name (the user whos AD values i want returned)

line 3 is split a little all return within the () is the Dsearch.Filter and everything after is from the try statement (the catch side of course).


You know Try..Catch, right? Therefore, the error should happen before TRY. Put breakpoint at the line before Try, then debug it step by step, you should be easy to find which line caused the error.


this will probably sound incredibly stupid but i am working on a dotnetnuke module and can not find anyway to run this outside of a web browser. web developer 05 allows me to add breakpoints but will not let me run the code independently...


Try to comment out line one by one. Start from Response.Write(Dsearch.Filter). You will find the line that causes the error.

if i dont declare a dsearch.filter i get the following

(objectClass=*)Error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

it looks as though either this line isnt pulling the right row...

Dsearch.Filter = "(& (objectClass=user)(cn=" + UserName + "))"

or this line isnt referring to a proper column

Dsearch.FindOne.Properties("postalCode")(0).ToString()

do you know a way to return the entire row? if so i could narrow it down to the first line... hopefully


I did Active Directory project long time before, I had some code as following:

search.Filter = "(SAMAccountName=" + userName + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
// Update the new path to the user in the directory
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];

Please search Active Directory and ASP.NET online to find helps.


could you walk me through this please?


Did you check to make sure zip code in AD called "ZipCode"? I though it called "postalCode".


Also, here is the forum on Active Directory. Please search your issue here.

http://forums.asp.net/93.aspx


i have tried both. neither seemed to work as a direct replacement.


che3358:

Did you check to make sure zip code in AD called "ZipCode"? I though it called "postalCode".

Do you know what the login name property is called within active directory?

0 comments:

Post a Comment