如何在c#中解决'索引超出数组范围'的问题 [英] How can solve ' Index was outside the bounds of the array' in c#

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

问题描述

[WebMethod]
   public static object ListUsersByRoles(string RoleName,int jtStartIndex = 0, int jtPageSize = 0)
   {
       try
       {
           int Id = 0;
           List<userdetails> Details = new List<userdetails>();
           MembershipUserCollection allUsers = Membership.GetAllUsers();
           var userList = Membership.GetAllUsers().OfType<MembershipUser>().ToList();
           if (RoleName == "Show All")
           {
               foreach (MembershipUser item in userList)
               {
                   var userRoles = Roles.GetRolesForUser(item.UserName);
                   Details.Add(new userdetails() { UserName = item.UserName, Email = item.Email, Comment = item.Comment, CreationDate = item.CreationDate.ToString("MM/dd/yyyy"), LastLoginDate = item.LastLoginDate.ToString("MM/dd/yyyy"), LastActivityDate = item.LastActivityDate.ToString("MM/dd/yyyy"), IsApproved = item.IsApproved.ToString(), IsOnline = item.IsOnline.ToString(), IsLockedOut = item.IsLockedOut.ToString(), Id = Convert.ToInt32(item.ProviderUserKey), RoleName = userRoles[0] });
               }
           }
           else
           {
               string[] usersInRole = Roles.GetUsersInRole(RoleName);
               foreach (MembershipUser item in userList)
               {
                   foreach (string userInRole in usersInRole)
                   {
                       if (userInRole == item.UserName)
                       {
                           var userRoles = Roles.GetRolesForUser(item.UserName);
                           Details.Add(new userdetails() { UserName = item.UserName, Email = item.Email, Comment = item.Comment, CreationDate = item.CreationDate.ToString("MM/dd/yyyy"), LastLoginDate = item.LastLoginDate.ToString("MM/dd/yyyy"), LastActivityDate = item.LastActivityDate.ToString("MM/dd/yyyy"), IsApproved = item.IsApproved.ToString(), IsOnline = item.IsOnline.ToString(), IsLockedOut = item.IsLockedOut.ToString(), Id =Convert.ToInt32(item.ProviderUserKey), RoleName = userRoles[0] });
                           break;
                       }
                   }
               }
           }
           int grpCount = Details.Count();
           var records = Details.Skip((jtStartIndex)).Take((jtPageSize));
           return new { Result = "OK", Records = records, TotalRecordCount = grpCount };
           }
           catch (Exception ex)
           {
               return new { Result = "ERROR", Message = ex.Message };
           }
   }

推荐答案

当您尝试访问不存在的数组项时会发生此错误在那个阵列?请搜索引发此错误的行并搜索哪个数组索引不存在
This error occur when you are trying to access an array item which is not present in that array ? Please search for the line which throws this error and search which array index is not exist


这篇关于如何在c#中解决'索引超出数组范围'的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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