Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5945133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:38:20+00:00 2026-05-22T16:38:20+00:00

Making myself a password manager and I’m running into some problems with a segment

  • 0

Making myself a password manager and I’m running into some problems with a segment of code. Whats supposed to happen is the application opens an xml file, and then populates a listview with the items contained in that xml document (the accounts). Right clicking on the listview gives a context menu for various options, all of which work fine individually. However, after opening the xml document, and then remove one of the accounts from the listview, then attempt to add another account, it throws the following:

 ArgumentOutOfRangeException unhandled.
 InvalidArgument=Value of '4' is not valid for 'index'.
 Parameter name: index

I’m assuming whats going wrong is when I remove the account from the listview, I’m messing up the count of the index variable which increments for every item in the xml document on application start. Not sure the best way to go about fixing that without breaking other portions of code. I was thinking of resetting the value of ‘index’ after removing an account by counting how many total items are now in the listView but not sure if thats best. Here’s what the code looks like when the xml is opened.

private void openPasswordFileToolStripMenuItem_Click(object sender, EventArgs e)
    {
        System.Xml.XmlDocument loadDoc = new System.Xml.XmlDocument();

        try
        {
            loadDoc.Load(Application.StartupPath + "\\database.xml");
        }
        catch (System.IO.FileNotFoundException)
        {
            MessageBox.Show("Password Database does not exist!");
        }
        foreach (System.Xml.XmlNode node in loadDoc.SelectNodes("/Database/Account"))
        {
            lvItem = listView1.Items.Insert(index, node.Attributes["Description"].InnerText); ;
            lvItem.SubItems.Add(new ListViewItem.ListViewSubItem(lvItem, node.Attributes["Username"].InnerText)); ;
            lvItem.SubItems.Add(new ListViewItem.ListViewSubItem(lvItem, node.Attributes["Password"].InnerText)); ;
            index += 1;
        }
    }

And finally the segment for removing the account:

 private void removeSelectedAccountToolStripMenuItem_Click(object sender, EventArgs e)
    {
        listView1.Items.Remove(listView1.SelectedItems[0]);
    }

Again everything works fine until the following sequence is executed: Accounts File Opened –> Account Removed –> Another Account added. At which point the exception is thrown and the new account is never added to the list view.

Here are the exception details. This is the ‘stack dump’?

  System.ArgumentOutOfRangeException was unhandled
  Message=InvalidArgument=Value of '3' is not valid for 'index'.
Parameter name: index
  Source=System.Windows.Forms
  ParamName=index
  StackTrace:
       at System.Windows.Forms.ListView.ListViewItemCollection.Insert(Int32 index, ListViewItem item)
       at System.Windows.Forms.ListView.ListViewItemCollection.Insert(Int32 index, String text)
       at PassKeeper.Form1.addAccountToolStripMenuItem_Click(Object sender, EventArgs e) in C:\Users\Hamann\documents\visual studio 2010\Projects\PassMan\PassMan\Form1.cs:line 35
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:38:21+00:00Added an answer on May 22, 2026 at 4:38 pm

    Since index is not declared in any method I saw, I assume that it is a class member. You are always incrementing index when adding accounts, but when removing them, it stays the same. So after removing an account, your ListView has fewer items in it than index suggests.

    The fix is simple. Get rid of index. It doesn’t look like you are using it for much anyway. In your foreach loop change your use of ListView.Items.Insert to ListView.Items.Add.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just working on some code and caught myself making this error if
I find myself making repetitive mistakes typing keywords and sentences in my code comments.
Debugging experience http://www.dmhermitage.org/wtfborders.png This is making me want to kill myself. I have some
I'm trying to make a webapp. The application I am making for myself to
I'm looking to save myself some effort further down the line by making a
I'm making a simple to-do list application to teach myself Ruby on Rails, however
I find myself constantly running into a situation where I have a set of
I am making a small project for myself that deals with gps and some
I'm making a batch watermarking tool for myself and some others at work and
After making some changes in my models (eg. new field in a model and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.