[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Nested DataReader - Child MenuItems - Help!!!

Posted on 2006-05-30
5
Medium Priority
?
218 Views
Last Modified: 2012-06-27
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
Comment
Question by:tbaseflug
  • 3
  • 2
5 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 16793736
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
 

Author Comment

by:tbaseflug
ID: 16794153
tried it and still apparently causing a memory leak
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 2000 total points
ID: 16794185
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
 

Author Comment

by:tbaseflug
ID: 16794271
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
 

Author Comment

by:tbaseflug
ID: 16794356
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question