Click here to Skip to main content
14,667,264 members
Rate this:
Please Sign up or sign in to vote.
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
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
Wes Aday 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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 2

Changes the Authentication in the Pro server, from Anonymous to Windows authentication
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100