Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to populate an array of arrays from a SqlDataReader, but in the end all rows of the array are filled with the same array of numbers.

Here's my T-SQL code:

SELECT 
    SUM(memberAttendants),
    Groups.Name,
    DATEPART(wk,convert(datetime,convert(varchar,StatisticsDate),104))
FROM 
    Stat.dbo.GroupStatistics 
INNER JOIN 
    Stat.dbo.Groups ON GroupStatistics.GroupID = Groups.GroupID 
WHERE 
    (Groups.OrganizationID = '4960') 
GROUP BY 
    Groups.Name, convert(datetime, convert(varchar, StatisticsDate), 104)
ORDER BY 
    Groups.Name, convert(datetime, convert(varchar, StatisticsDate), 104)

C# code:

SqlDataReader myReader1 = cmd1.ExecuteReader();

if (myReader1.HasRows)
{
    int i = 0, j = 0;
    string[] Name;
    Name = new string[100];
    int[][] MainArray = new int[100][];

    int[] ActivityArray;
    ActivityArray = new int[52];
    int[] WeekOfActivity;
    WeekOfActivity = new int[52];
    bool myFlag = false;

    for (int myCounter = 0; myCounter <= myReader1.FieldCount; myCounter++)
    {
        Aktivitet[myCounter] = new int[myCounter];
    }

    while (myReader1.Read())
    {
      if (!myReader1.IsDBNull(0) && !myReader1.IsDBNull(1))
      {
        if (i == 0 || myReader1.GetString(1) == Name[i-1]  || myFlag == true)
        {
           if (myFlag == true && myReader1.GetString(1) != Name[i-1])
           {
              i = 0;
              MainArray[j] = ActivityArray;
           }
           myFlag = false;
           ActivityArray[i] = myReader1.GetInt32(0);
           Name[i] = myReader1.GetString(1);
           i++;
       }
       else
       {
           MainArray[j] = ActivityArray;
           j++;
           i = 0;
           Name[i] = myReader1.GetString(1);
           ActivityArray[i] = myReader1.GetInt32(0);
           i++;
           myFlag = true;
       }
   }
}

MainArray[j] = ActivityArray;

How can I fill the MainArray without the last ActivityArray overwrite all the rows in MainArray?

Tnx

share|improve this question
    
You know you can work with DataTable having rows and columns... right? –  Shadow Wizard Aug 8 '13 at 10:52
    
@ShadowWizard ofcourse not right. :) –  Ehsan Aug 8 '13 at 10:53
    
@EhsanUllah sometimes people are aware of the proper way and just try different way for fun or weird project demands so just making sure. :) –  Shadow Wizard Aug 8 '13 at 10:54

1 Answer 1

up vote 0 down vote accepted

What you're missing here is that an array is a reference type. So when you say:

MainArray[j] = ActivityArray

You're setting the entry in MainArray to point to ActivityArray. You never create a new ActivityArray, so they all point to the same place.

Because you're over-writing the content of ActivityArray, you're effectively over-writing all entries in MainArray (because they are all the same entry).

So, you probably want

while (myReader1.Read())
{
    ActivityArray = new int[52];
    if (!myReader1.IsDBNull(0) && !myReader1.IsDBNull(1))
    {

The reason I have put probably is because it's not really that clear to me what your code is trying to do.

share|improve this answer
    
That's it! Tnx. What I try to do is to get different type of activities' attendees number of the week in the MainArray. Aktivities could be: Basketball, soccer, guitar, dancing... –  DevNewbie Aug 8 '13 at 13:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.