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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:27:44+00:00 2026-06-09T15:27:44+00:00

I have a WPF form in .Net framework 4.0. What I’m trying to do

  • 0

I have a WPF form in .Net framework 4.0. What I’m trying to do is to populate a combobox with the results of a query. I can do this but to prevent duplicating code I’m trying to have a class that I can use / call for each combobox I’d like to populate (multiple comboboxes will share results from the same query).

I’ve constructed my class as below:

 internal class DatabaseHandle
    {
        private string _connectionString =
            "Data Source=FINALLYWINDOWS7\\TESTING;Initial Catalog=Testing;Integrated Security=true";

        public string PopulateTeamMembers()
        {
            string queryString = "select    setting_main"
                                 + " from     [marlin].[support_config]"
                                 + " where  config_code = 30"
                                 + "         and setting_active = 1"
                                 + " order by setting_main";

            using (var connection = new SqlConnection(_connectionString))
            {
                var command = new SqlCommand(queryString, connection);

                try
                {
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        return reader[0].ToString();
                    }

                    reader.Close();
                }
                catch (Exception ex)
                {
                    return "Error, unable to load";
                }
            }
            return null;
        }
    }

And then in another class I have the following method:

private void LoadInterface()
{
    DatabaseHandle testing = new DatabaseHandle();
    string teammembers = testing.PopulateTeamMembers();

    comboBoxResolvedBy.Items.Add(teammembers);
}

This populates my combo box except that it only populates it with the first value from the query. I’ve checked the data, everything is correct on that front but I can’t seem to figure out how to enumerate over the query results from the LoadInterface method.

  • 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-06-09T15:27:46+00:00Added an answer on June 9, 2026 at 3:27 pm

    Note that when the function hits the return keyword, it exits from your function.
    This is why it’s only returning one value. It also isn’t calling reader.close.

    You should be returning multiple values in the form of an IEnumerable<string>, rather than a string.

    A way to solve this is to populate a list.

        public IEnumerable<string> PopulateTeamMembers()
        {
            List<string> returnList = new List<string>();
            string queryString = "select    setting_main"
                                 + " from     [marlin].[support_config]"
                                 + " where  config_code = 30"
                                 + "         and setting_active = 1"
                                 + " order by setting_main";
    
            using (var connection = new SqlConnection(_connectionString))
            {
                var command = new SqlCommand(queryString, connection);
    
                try
                {
                    connection.Open();
                    using(SqlDataReader reader = command.ExecuteReader())
                    {
                       while (reader.Read())
                       {
                           returnList.Add(reader[0].ToString());
                       }
    
                       reader.Close();
                    }
                }
                catch (Exception ex)
                {
                    // Just let the calling class handle the exception
                    throw;
                }
            }
            return returnList;
        }
    

    You can then call it like so:

       private void LoadInterface()
       {
           DatabaseHandle testing = new DatabaseHandle();
           IEnumerable<string> teammembers = testing.PopulateTeamMembers();
           foreach(string value in teammembers)
           {
               comboBoxResolvedBy.Items.Add(value);
           }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some applications developed in asp.net, WPF ,windows Form etc.How can i run
I have a WPF form where I'm trying to make a simple input form.
I have a WPF form with a combobox and a textbox (both are databound
In WPF / .Net, both System.Windows.Window and System.Windows.Forms.Form have the .ShowDialog method. The difference
I have WPF form with DataGrid. New columns can be added to the datagrid
I have WPF Form which has many buttons with the same code. Appearance of
I have a WPF form which contains 30x30 grid where each grid cell is
I have a WPF form that uses ClientLogin to log a user into their
I have a WPF form. It handles the KeyUp event and if the released
I have a main wpf form that opens a new form. However when that

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.