IndexOutOfRangeException:索引超出了数组的范围 [英] IndexOutOfRangeException: Index was outside the bounds of the array

查看:151
本文介绍了IndexOutOfRangeException:索引超出了数组的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我收到以下错误

Hi,

I am getting below error

IndexOutOfRangeException: Index was outside the bounds of the array

从Active Directory调用电子邮件ID时,这里是代码并跟随错误

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()





当我在Pro服务器中运行该工具时,如果我运行本地,则会发生这种情况。请帮助我,因为这是非常紧急的



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

推荐答案

您没有指出抛出此异常的行,但它可以是您为数组编制索引的任何位置: 名称[0] 名称[1] result.Properties [propertyName] [0 ] 。如果存在第0个或第1个元素,则永远不会检查。你需要检查一个数组至少有一个或两个元素。

另外,你需要检查元素 result.Properties [propertyName] 存在。

-SA
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


更改Pro服务器中的身份验证,从匿名到Windows身份验证
Changes the Authentication in the Pro server, from Anonymous to Windows authentication


这篇关于IndexOutOfRangeException:索引超出了数组的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆