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 8915727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:02:22+00:00 2026-06-15T05:02:22+00:00

(In this windows form application) I’m trying to read data from a file into

  • 0

(In this windows form application) I’m trying to read data from a file into a hash table and populate text boxes with the data in the hash table but when I run the code I’m always thrown the exception “Item has already been added. Key in dictionary: ” Key being added: ” “

Initial code:

string[] fileLines = File.ReadAllLines(@"C:path/ajand.txt");

foreach (string line in fileLines)
{
    // to split the first 9 chars in the string and use them as key values                
    string[] match = Regex.Split(line, line.Substring(0,9));
    hT.Add(match[0], line);
}

so i tried checking for key duplicates with the following code

 string[] fileLines = File.ReadAllLines(@"C:path/ajand.txt");

 foreach (string line in fileLines)
 {
     string[] match = Regex.Split(line, line.Substring(0,9));

     if(!hT.ContainsKey(match[0])) // to check duplicates
     hT.Add(match[0], line);            
 }

But when I run the program the corresponding text boxes are not populated with the data that “seems” to have been added to the hash table.
Please any ideas what the problem is.

  • 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-06-15T05:02:23+00:00Added an answer on June 15, 2026 at 5:02 am

    If I understand correctly you could use a function like this one:

    public static Dictionary<string, string> LoadActivityLookup(string filePath) {
        const int KEY_LENGTH = 10;
        var newLookup = new Dictionary<string, string>();
        foreach (string line in File.ReadAllLines(filePath)) {
            if (line.Length < KEY_LENGTH) continue;
            string key = line.Substring(0, KEY_LENGTH);
            if (!newLookup.ContainsKey(key)) {
                string value = line.Substring(KEY_LENGTH);
                newLookup.Add(key, value);
            }
        }
        return newLookup;
    }
    

    Dictionaries are great for repeatedly looking up keys in a large set of keys. But if you only need to hold a bunch of key/value pairs in a collection so that you can later iterate through them, I would go with a List<> instead.

    Here is a version of the above function which uses a StreamReader instead of loading the complete file in a string array.

    public static Dictionary<string, string> LoadActivityLookup(string filePath) {
        const int KEY_LENGTH = 10;
        var newLookup = new Dictionary<string, string>();
        using (StreamReader rdr = new StreamReader(filePath)) {
            string line = rdr.ReadLine();
            while (line != null) {
                if (line.Length < KEY_LENGTH) {
                    line = rdr.ReadLine();
                    continue;
                } 
                string key = line.Substring(0, KEY_LENGTH);
                if (!newLookup.ContainsKey(key)) {
                    string value = line.Substring(KEY_LENGTH);
                    newLookup.Add(key, value);
                }
                line = rdr.ReadLine();
            }
        }
        return newLookup;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this application (Windows form application written in Visual C++) from a colleague
I'm making a C# windows form application that needs to parse data from an
I'm trying to make an Aduto-Forum poster with VB.NET windows form application This is
I have C# windows form application that queries data from a connected device. I
I have a C# Windows Form application that contains a menu with this event:
I am using C# with a Windows Application Form. In this I have a
I have a windows form without running it in an application. this is the
I am writing this app in windows form c#. When user exit from this
I have a Windows Form application. What this application does, is let the user
net windows form application. I have a combo box and a text box 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.