Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,

I am getting below error
IndexOutOfRangeException: Index was outside the bounds of the array
while calling the Email id from Active Directory, here is the code and follows the error
C#
string propertyName = "mail";
               string User = HttpContext.Current.User.Identity.Name; // System.Environment.UserName;

               string[] Name = Regex.Split(User.Trim(), @"\\");
               string username = Name[1];
               //string domainname = HttpReq//System.Environment.UserDomainName.ToString().ToLower();
               string domainname = Name[0]; //"AsiaPacific";
               DirectoryEntry entry = new DirectoryEntry("LDAP://DC=" + domainname + ",DC=cpqcorp,DC=net");
               DirectorySearcher search = new DirectorySearcher(entry);
               search.Filter = "(&(&(objectClass=user)(SamAccountName=" + username + ")))";
               search.PropertiesToLoad.Add(propertyName);
               SearchResult result = search.FindOne();
               string propertyValue = "";
               if (result != null)
               {
                   propertyValue = result.Properties[propertyName][0].ToString();
                   //propertyValue = result.Properties.Count.ToString();

               }
               return propertyValue;
Exception Error:
[IndexOutOfRangeException: Index was outside the bounds of the array.]
   clsCommon.DisplayName()


This is happening when i run the tool in Pro server not coming in if i run local. Please help me as this is very urgent
Posted
Updated 22-Oct-17 9:27am
v4
Comments
[no name] 20-Oct-14 10:57am    
No it's not urgent at all. Why do you assume that Name contains anything at all much less more that one string in the array?
Sergey Alexandrovich Kryukov 20-Oct-14 11:09am    
In what line?
Use the debugger and you will see. The problem is very simple.
—SA
Rehman2803 20-Oct-14 11:25am    
I am not able finding the line as it was working if i put debug in local. only in Published server it is throwing as exception
Sergey Alexandrovich Kryukov 20-Oct-14 11:45am    
This is so basic debugging technique everyone should be able to use. No one will debug your code for you.
Besides, you could get this information from exception stack.
I answered your question.
—SA
Rehman2803 21-Oct-14 6:25am    
solved myself, Changed the Authentication in the Pro server, from Anonymous to Windows authentication, thanks for you time

You did not indicate the line where you have this exception thrown, but it can be any place where you index the arrays: Name[0], Name[1] and result.Properties[propertyName][0]. You never check up if the 0-th or 1-st element exists. You need to check up of an array has at least one or two elements.
Also, you need to check up that the element result.Properties[propertyName] exists.
—SA
 
Share this answer
 
v2
Comments
Rehman2803 20-Oct-14 11:23am    
result.Properties[propertyName][0] getting the email id as expected and this error is not coming if i run in local, in Pro server only i am getting the same.
I am not able finding the line as it was working if i put debug. But in Published server it is throwing as exception
Sergey Alexandrovich Kryukov 20-Oct-14 11:46am    
At least fix the bugs I indicated in my answer. Consider accepting the answer formally (green "Accept") button.
—SA
Changes the Authentication in the Pro server, from Anonymous to Windows authentication
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900