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 3794364
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:55:06+00:00 2026-05-19T12:55:06+00:00

I’m trying to move some of the code behind in my project to a

  • 0

I’m trying to move some of the code behind in my project to a separate class. The part that I am interested in moving is the GetData method. I would like to move it out of my code behind to a separate class called DataAccess. I would appreciate your assistance. Thanks!

MainWindow.xaml.cs

namespace Contact_Interests
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private ObservableCollection<AggregatedLabel> myAggLabels;

    public ObservableCollection<AggregatedLabel> AggLabels
    {
        get { return myAggLabels; }
    }

    private ObservableCollection<ContactList> myContactLists;

    public IEnumerable<ContactList> ContactLists
    {
        get { return myContactLists; }
    }

    public MainWindow()
    {
        GetData();
        this.InitializeComponent();

        // Insert code required on object creation below this point.
    }

    public void GetData()
    {
        myAggLabels = new ObservableCollection<AggregatedLabel>();
        myContactLists = new ObservableCollection<ContactList>();

        DB2Connection conn = null;
        try
        {
            conn = new DB2Connection("XXXX;");
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message + " " + ex.InnerException);
        }

        //get all contactLists and their labels
        DB2Command command = new DB2Command("SQL SELECT statement");
        command.Connection = conn;

        conn.Open();

        //get all labels from database
        using (DB2DataReader dr = command.ExecuteReader())
        {
            while (dr.Read())
            {
                AggregatedLabel aggLabel = new AggregatedLabel();

                aggLabel.ID = Convert.ToInt32(dr["LABEL_ID"]);
                aggLabel.Name = dr["LABEL_NAME"].ToString();

                myAggLabels.Add(aggLabel);

            }
        }
        //Add unique contactLists to dictionary
        Dictionary<int, ContactList> myContactDictionary = new Dictionary<int, ContactList>();

        using (DB2DataReader dr = command.ExecuteReader())
        {
            while (dr.Read())
            {
               int id = Convert.ToInt32(dr["CONTACT_LIST_ID"]);

                if (!myContactDictionary.ContainsKey(id))
                {

                    ContactList contactList = new ContactList();

                    contactList.ContactListID = id;
                    contactList.ContactListName = dr["CONTACT_LIST_NAME"].ToString();
                    contactList.AggLabels = new ObservableCollection<AggregatedLabel>()
                {
                    new AggregatedLabel()
                    {
                        ID = Convert.ToInt32(dr["LABEL_ID"]),
                        Name = dr["LABEL_NAME"].ToString()
                    }

                };
                    myContactDictionary.Add(id, contactList);
                }
                else
                {
                    //populate existing contact lists with remaining labels
                    ContactList contactList = myContactDictionary[id];

                    contactList.AggLabels.Add
                    (
                        new AggregatedLabel() 
                        {
                            ID = Convert.ToInt32(dr["LABEL_ID"]),
                            Name = dr["LABEL_NAME"].ToString()
                        }
                    );
                }
            }
        }

        //add to observable collection      
        foreach (KeyValuePair<int, ContactList> contactKeyValue in myContactDictionary)
        {
            ContactList contactList = contactKeyValue.Value;

            myContactLists.Add(contactList);
        }

        conn.Close();        
    }
}
  • 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-05-19T12:55:06+00:00Added an answer on May 19, 2026 at 12:55 pm

    You should make a class for this, and just move all of your code into it.

    Then create an instance of the class and set it as the DataContext for this Window.

    If your interested in the mechanics, and the motivation (other than just pulling out of code behind), you can refer to my series on MVVM, or one of the many great MVVM introductions.


    For example, your code above could be:

    namespace Contact_Interests
    {
        public partial class MainWindowViewModel // : INotifyPropertyChanged
        {
            private ObservableCollection<AggregatedLabel> myAggLabels;
    
            public ObservableCollection<AggregatedLabel> AggLabels
            {
                get { return myAggLabels; }
            }
    
            private ObservableCollection<ContactList> myContactLists;
    
            public IEnumerable<ContactList> ContactLists
            {
                get { return myContactLists; }
            }
    
            public MainWindowViewModel()
            {
                GetData();
    
                // Insert code required on object creation below this point.
            }
    
            public void GetData()
            {
                myAggLabels = new ObservableCollection<AggregatedLabel>();
                myContactLists = new ObservableCollection<ContactList>();
    
                DB2Connection conn = null;
                try
                {
                    conn = new DB2Connection("XXXX;");
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(ex.Message + " " + ex.InnerException);
                }
    
                //get all contactLists and their labels
                DB2Command command = new DB2Command("SQL SELECT statement");
                command.Connection = conn;
    
                conn.Open();
    
                //get all labels from database
                using (DB2DataReader dr = command.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        AggregatedLabel aggLabel = new AggregatedLabel();
    
                        aggLabel.ID = Convert.ToInt32(dr["LABEL_ID"]);
                        aggLabel.Name = dr["LABEL_NAME"].ToString();
    
                        myAggLabels.Add(aggLabel);
    
                    }
                }
                //Add unique contactLists to dictionary
                Dictionary<int, ContactList> myContactDictionary = new Dictionary<int, ContactList>();
    
                using (DB2DataReader dr = command.ExecuteReader())
                {
                    while (dr.Read())
                    {
                       int id = Convert.ToInt32(dr["CONTACT_LIST_ID"]);
    
                        if (!myContactDictionary.ContainsKey(id))
                        {
    
                            ContactList contactList = new ContactList();
    
                            contactList.ContactListID = id;
                            contactList.ContactListName = dr["CONTACT_LIST_NAME"].ToString();
                            contactList.AggLabels = new ObservableCollection<AggregatedLabel>()
                        {
                            new AggregatedLabel()
                            {
                                ID = Convert.ToInt32(dr["LABEL_ID"]),
                                Name = dr["LABEL_NAME"].ToString()
                            }
    
                        };
                            myContactDictionary.Add(id, contactList);
                        }
                        else
                        {
                            //populate existing contact lists with remaining labels
                            ContactList contactList = myContactDictionary[id];
    
                            contactList.AggLabels.Add
                            (
                                new AggregatedLabel() 
                                {
                                    ID = Convert.ToInt32(dr["LABEL_ID"]),
                                    Name = dr["LABEL_NAME"].ToString()
                                }
                            );
                        }
                    }
                }
    
                //add to observable collection      
                foreach (KeyValuePair<int, ContactList> contactKeyValue in myContactDictionary)
                {
                    ContactList contactList = contactKeyValue.Value;
    
                    myContactLists.Add(contactList);
                }
    
                conn.Close();        
            }
        }
    }
    

    Then, your main window becomes:

    namespace Contact_Interests
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
             public MainWindow()
             {
                  InitializeComponent();
                  this.DataContext = new MainWindowViewModel();
             }
         }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.