0

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

3
  • You know you can work with DataTable having rows and columns... right? Commented Aug 8, 2013 at 10:52
  • @ShadowWizard ofcourse not right. :) Commented Aug 8, 2013 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. :) Commented Aug 8, 2013 at 10:54

1 Answer 1

0

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.

1
  • 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... Commented Aug 8, 2013 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.