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

  • SEARCH
  • Home
  • 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 7930445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:25:37+00:00 2026-06-03T20:25:37+00:00

Okay, so you got the idea from title, let me just post the code

  • 0

Okay, so you got the idea from title, let me just post the code and in comments I’ll explain what’s happening (and what should!)

// select distinct subject from database
cmd.CommandText = "SELECT DISTINCT SubjectId  FROM ClassSubject";
// a new command
OleDbCommand cmdTemp = new OleDbCommand();
// con id defined earlier
cmdTemp.Connection = con;
// Reader for outer loop
OleDbDataReader rdr;
// reader for inner loop
OleDbDataReader rdrTemp;
// reader for main command, that is for each subject
rdr = cmd.ExecuteReader();
// this will store subject id
int nTempID;
// this is the list that is supposed to contain all the classes (many to many relation btw class and subject)
// it does gets set fine, but later only 11 items show up
List<Int32> lstTempSub = new List<int>();
// a dictionary that will store all classes for this each particular subject
_clsSub = new Dictionary<int, List<int>>();
// read main, i.e. read subjects
while (rdr.Read())
{
    // set the id of subject
    nTempID = rdr.GetInt32(0);
    // clear previous items
    lstTempSub.Clear();
    // this selects all the classes for this particular subject
    cmdTemp.CommandText = "SELECT ClassId FROM ClassSubject WHERE SubjectID=" + nTempID + " ORDER BY ClassID";
    // Execute in the tempReader
    rdrTemp = cmdTemp.ExecuteReader();
    // read
    while (rdrTemp.Read())
    {
        // here, we add all the classes that are there for this particular subject to the list we createed
        lstTempSub.Add(rdrTemp.GetInt32(0));
    }
    // close inner one
    rdrTemp.Close();
    // every thing is fine till here,
    // i.e. nTempId is what is should be, the id of this particular subect
    // lstTempSub contains all class for this subject, may be 1 or may be 100
    _clsSub.Add(nTempID, lstTempSub);
}
// close outer one
rdr.Close();

// Here is where things get wrong
 foreach(KeyValuePair<Int32, List<Int32>> pair in _clsSub)
            {
                // when the debugger gets here, the key is fine for each subject
                // but pair.Value always have 11 values at most, if for a subject
                // there were less class, then it shows okay,
                // but in case there were more than 11, only 11 values are shown in pair.value
                SomeMethod(pair.Key, pair.Value);
            }

I Guess I explained everything in the code comments, but if you still need clarification on anything, feel free to ask. But basically, like I said, when I get to SomeMethod, the List doesn’t seems to maintain its items (if more then 11) (is this odd or what?)
So, Can anyone please help me with this? Any help would be appreciated.

UPDATE: Its not that 11 items are getting stored, its that whatever is the number of classes for last subject, that number of items are getting stored. Say if in upper loop, last id of subject is 52, and for that id there are 23 classes, then for every record inserted, even before, the _clsSub value’s list will have at most 23 items.

  • 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-03T20:25:39+00:00Added an answer on June 3, 2026 at 8:25 pm

    You declare this once:

    List<Int32> lstTempSub = new List<int>();
    

    Since it’s a reference type, whenever you’re using it, you’re actually using a reference to it. So when you do this in your loop:

    _clsSub.Add(nTempID, lstTempSub);
    

    You’re adding the same list to every slot in the dictionary. And when you do this:

    lstTempSub.Clear();
    

    You’re clearing that same list in every slot in the dictionary. As such, all the lists in your dictionary are the same list, and end up containing what was in the last loop (that is, the last time you cleared it and added to it).

    You need to move List<Int32> lstTempSub = new List<int>(); into the while loop in place of the line lstTempSub.Clear(); and everything will work.

    (In answer to your title question in case it wasn’t obvious – the last thing you loop through has 11 items, so all of them seem to have 11 items because they point to that same one)

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

Sidebar

Related Questions

I run this query: SELECT u.user_id, u.fname, u.lname, n.title, n.news_id, n.post, n.zip, z.city,z.state_abbr FROM
I'm checking my code for memory leaks. Everything is okay until I got the
Okay, so I've got the following code shown below to create a dialog using
Okay, so I've got a strange problem with the following Lua code: function quantizeNumber(i,
Okay, so I've got some C code to perform a mathematical operation which could,
Okay, so we've got this event. Awesome, right? We've also got a timer with
Okay, so I've got a problem - and I'd love to have it fixed.
Okay, I've got my normal app which is in portrait mode. I can force
Okay, so I've got a Postgres database hosted on a VPS at the moment,
Okay, I think I'm just making a stupid mistake here, but I want to

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.