• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Nested DataReader - Child MenuItems - Help!!!

OK - ihave the followign code - whose intention it is to list a series of menuitems and then nest the child munitems under the - this is a win app (well, mobile app, but close enough) - I am sure that my logic is fundamentally flowed, so any help would be grealty appreciate - please let me know if I need to provide anything else?

        private void LoadAccounts()
        {
            SqlCeDataReader dr = null;
            SqlCeDataReader dr2 = null;
            try
            {
                dr = data.LoadAccountNamesAndIDs();
                while (dr.Read())
                {
                    MyMenuItem miAccountName = new MyMenuItem();
                    miAccountName.Text = dr["AccountName"].ToString();
                    miAccountName.Tag = dr["AccountID"].ToString();
                    miAccount.MenuItems.Add(miAccountName);

                    dr2 = data.LoadAccountLocations(miAccountName.Tag);
                    if (dr2.Read())
                    {
                        MyMenuItem mainthreadNode = new MyMenuItem();
                        mainthreadNode.Text = "All Items";
                        mainthreadNode.Tag = dr["AccountID"].ToString();
                        miAccountName.MenuItems.Add(mainthreadNode);
                        mainthreadNode.Click += new EventHandler(mi_Click);

                        while (dr2.Read())
                        {
                            MyMenuItem threadNode = new MyMenuItem();
                            threadNode.Text = dr2["LocationName"].ToString();
                            threadNode.Tag = dr["AccountID"].ToString();
                            miAccountName.MenuItems.Add(threadNode);
                            threadNode.Click += new EventHandler(mi_Click);
                        }
                    }
                    else
                    {
                        miAccountName.Enabled = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not load Account Names: " +
                  ex.Message + ")!", "RIO Data Load Error");
            }
            finally
            {
                if (dr != null)
                {
                    dr.Dispose();
                    dr.Close();

                    if (dr2 != null)
                    {
                        dr2.Dispose();
                        dr2.Close();
                    }
                }
            }
        }
0
tbaseflug
Asked:
tbaseflug
  • 3
  • 2
1 Solution
 
dstanley9Commented:
try adding the node after adding all sub-nodes:

        private void LoadAccounts()
        {
            SqlCeDataReader dr = null;
            SqlCeDataReader dr2 = null;
            try
            {
                dr = data.LoadAccountNamesAndIDs();
                while (dr.Read())
                {
                    MyMenuItem miAccountName = new MyMenuItem();
                    miAccountName.Text = dr["AccountName"].ToString();
                    miAccountName.Tag = dr["AccountID"].ToString();

                    dr2 = data.LoadAccountLocations(miAccountName.Tag);
                    if (dr2.Read())
                    {
                        MyMenuItem mainthreadNode = new MyMenuItem();
                        mainthreadNode.Text = "All Items";
                        mainthreadNode.Tag = dr["AccountID"].ToString();
                        miAccountName.MenuItems.Add(mainthreadNode);
                        mainthreadNode.Click += new EventHandler(mi_Click);

                        while (dr2.Read())
                        {
                            MyMenuItem threadNode = new MyMenuItem();
                            threadNode.Text = dr2["LocationName"].ToString();
                            threadNode.Tag = dr["AccountID"].ToString();
                            miAccountName.MenuItems.Add(threadNode);
                            threadNode.Click += new EventHandler(mi_Click);
                        }
                    }
                    else
                    {
                        miAccountName.Enabled = false;
                    }
                    miAccount.MenuItems.Add(miAccountName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not load Account Names: " +
                  ex.Message + ")!", "RIO Data Load Error");
            }
            finally
            {
                if (dr != null)
                {
                    dr.Dispose();
                    dr.Close();

                    if (dr2 != null)
                    {
                        dr2.Dispose();
                        dr2.Close();
                    }
                }
            }
        }
0
 
tbaseflugAuthor Commented:
tried it and still apparently causing a memory leak
0
 
dstanley9Commented:
Memory leak?  I didn't know that was the problem...  I'm afraid I can't help much with memory management on a Mobile App.  How many data points are you loading?  You might also dispose of your DAtaReaders earlier by using nested try..catch..finally blocks instead of one big block.
0
 
tbaseflugAuthor Commented:
Actually, I am not 100% certain it is a leak - it is throwing a native error so am havng a hard time catching it, but did track it down to this code block - so was looking for any generic assistance in optimizing the code as is, etc.
0
 
tbaseflugAuthor Commented:
Ok - i tried nesting the try..catch and so far so good - will test a little more, but this is the first positive sign that I have had in a few days
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now