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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:55:34+00:00 2026-06-16T14:55:34+00:00

I have a combobox which is called combobox1 , which I want to populate

  • 0

I have a combobox which is called combobox1, which I want to populate it with id as value and Name as display name. I searched and read some tutorial and found this code to use in Form load event, but it doesn’t populate the list. I see an empty dropdown. Any ideas of where I am wrong?

In my database class I have this function.

public static void FillDropDownList(string Query, System.Windows.Forms.ComboBox DropDownName)
{
   SqlDataReader dr;

   SqlConnection myConnection = new SqlConnection(CONNECTION_STRING);
   try
   {
      myConnection.Open();
   }
   catch (Exception e)
   {
      Console.WriteLine(e.ToString());
   }

   // Check whether the Drop Down has existing items. If YES, empty it.
   if (DropDownName.Items.Count > 0)
      DropDownName.Items.Clear();

   SqlCommand cmd = new SqlCommand(Query, myConnection);
   dr = cmd.ExecuteReader();

   while (dr.Read())
      DropDownName.Items.Add(dr[0].ToString());

   Console.Write(DropDownName.Items.Add(dr[0].ToString()));
   dr.Close();
}

In my form i call it as

private void sales_record_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(DBUtils.CONNECTION_STRING);
    DBUtils.FillDropDownList("select id,Name from Farms", comboBox1);
}
  • 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-16T14:55:35+00:00Added an answer on June 16, 2026 at 2:55 pm

    My advice – use .NET built-in functionality as much as possible, and don’t handle data binding manually (which is what you are trying to do in your code):

    1. Use ExecuteQuery to pull a DataTable from database.
    2. Set DropDownName.DataSource = yourDataTable.
    3. Set DropDownName.ValueMember = “id”.
    4. Set DropDownName.DisplayMember = “Name”.

    So your code would look similar to this:

    public static void FillDropDownList(string Query, System.Windows.Forms.ComboBox DropDownName)
    {
      DataTable dt;
    
      using (var cn = new SqlConnection(CONNECTION_STRING))
      {
        cn.Open();
    
        try
        {
          SqlCommand cmd = new SqlCommand(Query, cn);
          dt = cmd.ExecuteQuery();
        }
        catch (SqlException e)
        {
          Console.WriteLine(e.ToString());
          return;
        }
      }
    
      DropDownName.DataSource = dt;
      DropDownName.ValueMember = "id";
      DropDownName.DisplayMember = "Name";
    }
    

    Notice how I changed exception type to SqlException, so we are only looking for database errors. Everything else will blow up. I don’t remember any situation when myConnection.Open(); would throw an exception, so your try block is not very useful. Notice in my try clause – it has ExecuteQuery inside it, which is likely to fail.

    EDIT: There is no need to close connection in the finally block when using the using construct. So it can be removed – and your code becomes more compact as a result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have DataTable containing necessary DataRows , which I want to display in ComboBox
I have an ExtJS combobox, which is a required field, but I don't want
I have combobox which id name is option and id name is Chk and
I have a combobox which has ItemsSource set to an ObservableCollection property called DATA
What I want is to have a ComboBox which, upon SelectedIndexChanged , changes a
I have a combobox which is connected to the database so I populate the
i have a combobox which is displaying date from database...in my database the date
I have a combobox which is populated by the Keys enumeration (winforms). The problem
I have a combobox from which i need to programmatically disable items depending on
I have a datagridview and a combobox which get populated randomly with data. However,

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.