Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Index was out of range. Must be non-negative and less than the size of the collection error when using checkbox list

Posted on 2009-05-05
10
Medium Priority
?
685 Views
Last Modified: 2013-12-17
I have a checkbox list that I use to assign membership accounts to roles.  However if I don't check a role in the checkbox list I get

Index was out of range. Must be non-negative and less than the size of the collection

See my code below
foreach (ListItem rolebox in UserRoles.Items)
            {
                if (rolebox.Selected)
                {
                    if (!Roles.IsUserInRole(UserName, rolebox.Text))
                    {
                        Roles.AddUserToRole(UserName, rolebox.Text);
                        if (rolebox.Text == "test")
                        {
					//then add to community server                                                         Communitytest.CreateCommunityServerAccount(UserName);
                        }
                    }
                }
                else 
                {
                    if (Roles.IsUserInRole(UserName, rolebox.Text))
                    {
                        Roles.RemoveUserFromRole(UserName, rolebox.Text);
                        //remove from community server                        if (rolebox.Text == "Advocate")
                        {
                            Communitytest.RemoveCommunityServerAccount(UserName);
                        }
                    }
                }
            }

Open in new window

0
Comment
Question by:mugsey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 6

Expert Comment

by:DanielWillmott
ID: 24305007
What line is the error happening on?
0
 
LVL 9

Expert Comment

by:tculler
ID: 24305011
Well, first things first, on line 19, was that a copy/paste error, or does your code really have that if-statement condition commented out? If it is commented out, that may be the source. The brackets are still valid, but it will just always execute that RemoveCommunityServerAccount method call, which may be throwing the exception.
0
 

Author Comment

by:mugsey
ID: 24305129
Sorry it was a cut and paste job.  Both of the

if (rolebox.Text == "test")

should say "test" and not advocate.  

I think the problem is when no checkbox is selected and I try and do the update?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:DanielWillmott
ID: 24305147
But where is the error happening? There doesn't seem to be anything wrong here. I'm wondering is there is a problem in the function Communitytest.RemoveCommunityServerAccount().
0
 

Author Comment

by:mugsey
ID: 24305212
Yes I will have a look and check
0
 

Author Comment

by:mugsey
ID: 24305689
Yes it is with the RemoveCommunityServerAccount method call.

Now its a web service that is failing when returning null.  So I need to check if its null so I can handle it.

I have  

CSUser = CommunityServerWebService.GetUsers(query)[0];

but that is were I am getting the error

I am doing this here
http://dev.communityserver.com/forums/p/506234/646354.aspx#646354



0
 
LVL 6

Expert Comment

by:DanielWillmott
ID: 24305770
Could you post some more code where the 'CSUser = CommunityServerWebService.GetUsers(query)[0];' line is located?
 
0
 

Author Comment

by:mugsey
ID: 24305850
Yes here you are

 
 
MembershipService NPWebSservice = new MembershipService(CommunityServerURL, "admin", CSapiKey);
        MembershipUser mu = Membership.GetUser(userName);
        if (mu != null)
        {
            User newCSUser;
            UserQuery query = new UserQuery();
            query.Usernames.Add(userName);
            newCSUser = NPWebSservice.GetUsers(query).FirstOrDefault(o => o.Username == userName);
            //existingCSUser = NPWebSservice.GetUsers(query)[0];
            
            if (newCSUser == null)
            {
                newCSUser = new User();
                newCSUser.Username = mu.UserName;
                etc etc...
 
                // add to community server
                User createdCSUser = NPWebSservice.AddUser(newCSUser);                
                createdCSUser.Status.AccountStatus = UserAccountStatus.Approved;
                etc etc
                NPWebSservice.UpdateUser(createdCSUser);
            }
        }

Open in new window

0
 

Author Comment

by:mugsey
ID: 24305872
Sorry I put back the line

existingCSUser = NPWebSservice.GetUsers(query)[0];

as per the documentation but I need to check for null if no records exist
0
 
LVL 6

Accepted Solution

by:
DanielWillmott earned 2000 total points
ID: 24310640
I'm not totally sure I'm following what you're trying to do here, but it this what you mean?

MembershipService NPWebSservice = new MembershipService(CommunityServerURL, "admin", CSapiKey);
MembershipUser mu = Membership.GetUser(userName);
if (mu != null)
{
    User newCSUser;
    UserQuery query = new UserQuery();
    query.Usernames.Add(userName);
    User existingCSUser = NPWebSservice.GetUsers(query)[0].FirstOrDefault(o => o.Username == userName);
    
    if (existingCSUser == null)
    {
        newCSUser = new User();
        newCSUser.Username = mu.UserName;
        etc etc...
 
        // add to community server
        User createdCSUser = NPWebSservice.AddUser(newCSUser);                
        createdCSUser.Status.AccountStatus = UserAccountStatus.Approved;
        etc etc
        NPWebSservice.UpdateUser(createdCSUser);
    }
}

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question