Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Populating combobox with values from Table using C#

Populating combobox with values from Table using C#

Scheduled Pinned Locked Moved C#
databasecsharpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    haroon1980
    wrote on last edited by
    #1

    Hi, I have following function below but is not populating values from the table product?

    private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
    try
    {

                database = new OleDbConnection(connectionString);
    
                // Create a command object and then a query which accepts a papameter
                System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select \* from Products", database);
                database.Open();
                System.Data.OleDb.OleDbDataReader dr;
                dr=cmd.ExecuteReader();
    
                while (dr.Read())
                {
                    //comboBox3.Items.AddRange(new object\[\] { dr\[1\].ToString() });  //Change the index according to the values you want to populate in the combobox.
                    comboBox3.Items.Add(dr\["ProductName"\]);
    
                }
            }
                catch (Exception ex)
            {
    
                MessageBox.Show(ex.Message);
                return;
            }
                
               
               }
    

    regards,

    J Y 2 Replies Last reply
    0
    • H haroon1980

      Hi, I have following function below but is not populating values from the table product?

      private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
      {
      try
      {

                  database = new OleDbConnection(connectionString);
      
                  // Create a command object and then a query which accepts a papameter
                  System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select \* from Products", database);
                  database.Open();
                  System.Data.OleDb.OleDbDataReader dr;
                  dr=cmd.ExecuteReader();
      
                  while (dr.Read())
                  {
                      //comboBox3.Items.AddRange(new object\[\] { dr\[1\].ToString() });  //Change the index according to the values you want to populate in the combobox.
                      comboBox3.Items.Add(dr\["ProductName"\]);
      
                  }
              }
                  catch (Exception ex)
              {
      
                  MessageBox.Show(ex.Message);
                  return;
              }
                  
                 
                 }
      

      regards,

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      - you probably want to clear out the old values from the combo box before you populate it with new data - don't forget to close dr and database after you're finished using them otherwise you'll leak resources - what happens when you step through this in the debugger? does your while loop properly iterate through all the rows in the DB? - if the only column you're using from the DB table is ProductName then doing a Select * is rather inefficient. - just for my own curiosity, why are you doing this in the SelectedIndexChanged event handler for the combo box?


      Last modified: after originally posted -- grammar correction

      H 1 Reply Last reply
      0
      • J Jimmanuel

        - you probably want to clear out the old values from the combo box before you populate it with new data - don't forget to close dr and database after you're finished using them otherwise you'll leak resources - what happens when you step through this in the debugger? does your while loop properly iterate through all the rows in the DB? - if the only column you're using from the DB table is ProductName then doing a Select * is rather inefficient. - just for my own curiosity, why are you doing this in the SelectedIndexChanged event handler for the combo box?


        Last modified: after originally posted -- grammar correction

        H Offline
        H Offline
        haroon1980
        wrote on last edited by
        #3

        Hi Jimmanuel, ...What else method or way u recommend instead of SelectedIndexChanged ? can u pls gimme example? ... I have 3 fields in that table so why it is inefficient? i have changed code and following is here: to include dr.close method..

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
        try
        {

                    database = new OleDbConnection(connectionString);
        
                    // Create a command object and then a query which accepts a papameter
                    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select distinct ProductName from Products", database);
                    database.Open();
                    System.Data.OleDb.OleDbDataReader dr;
                    dr = cmd.ExecuteReader();
        
                    while (dr.Read())
                    {
                        //comboBox3.Items.AddRange(new object\[\] { dr\[1\].ToString() });  //Change the index according to the values you want to populate in the combobox.
                        comboBox3.Items.AddRange(new Object\[\] { dr\[1\].ToString() });
                        
                    }
                    dr.Close();
                }
                catch (Exception ex)
                {
        
                    MessageBox.Show(ex.Message);
                    return;
                }
        

        }

        please help

        J 1 Reply Last reply
        0
        • H haroon1980

          Hi, I have following function below but is not populating values from the table product?

          private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
          {
          try
          {

                      database = new OleDbConnection(connectionString);
          
                      // Create a command object and then a query which accepts a papameter
                      System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select \* from Products", database);
                      database.Open();
                      System.Data.OleDb.OleDbDataReader dr;
                      dr=cmd.ExecuteReader();
          
                      while (dr.Read())
                      {
                          //comboBox3.Items.AddRange(new object\[\] { dr\[1\].ToString() });  //Change the index according to the values you want to populate in the combobox.
                          comboBox3.Items.Add(dr\["ProductName"\]);
          
                      }
                  }
                      catch (Exception ex)
                  {
          
                      MessageBox.Show(ex.Message);
                      return;
                  }
                      
                     
                     }
          

          regards,

          Y Offline
          Y Offline
          Yusuf
          wrote on last edited by
          #4

          why are you populating comboBox3 from db when something got selected on the same combobox? You know showing any other thing except populating your combobox. You need to do this only once or when ever the data changes.

          Yusuf

          H 1 Reply Last reply
          0
          • H haroon1980

            Hi Jimmanuel, ...What else method or way u recommend instead of SelectedIndexChanged ? can u pls gimme example? ... I have 3 fields in that table so why it is inefficient? i have changed code and following is here: to include dr.close method..

            private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
            {
            try
            {

                        database = new OleDbConnection(connectionString);
            
                        // Create a command object and then a query which accepts a papameter
                        System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select distinct ProductName from Products", database);
                        database.Open();
                        System.Data.OleDb.OleDbDataReader dr;
                        dr = cmd.ExecuteReader();
            
                        while (dr.Read())
                        {
                            //comboBox3.Items.AddRange(new object\[\] { dr\[1\].ToString() });  //Change the index according to the values you want to populate in the combobox.
                            comboBox3.Items.AddRange(new Object\[\] { dr\[1\].ToString() });
                            
                        }
                        dr.Close();
                    }
                    catch (Exception ex)
                    {
            
                        MessageBox.Show(ex.Message);
                        return;
                    }
            

            }

            please help

            J Offline
            J Offline
            Jimmanuel
            wrote on last edited by
            #5

            How often is the DB Table going to change? If it doesn't change or rarely changes then you really only need to populate the combo box values once - when the form loads. If it does change frequently then there are lots of good ways to keep the combo box up to date. You could use a timer to reload the values every so often, put a button on the form that allows the user to refresh whenever they want or work out some way of receiving a notification any time that the table changes. database.Close() is just as important. That closes the connection to the DB, otherwise it gets orphaned. A using[^] statement would be helpful in making sure that everything gets cleaned up whether an exception is thrown or not. Say your table has 10 rows; that means that all you want from it is 10 product names but a select * statement is giving you back the 10 product names plus 20 other values that you aren't using. That's 3 times as much network traffic as you actually need. It might not be a big deal now but what if there were 10,000 rows? What if later on the table changes and more columns get added? For each column that's added that's more unnecessary traffic between your app and your DB and more time and memory required to handle your select. Select distinct ProductName is the way to go here. In fact, I can't recall a time that I've ever used select *.

            1 Reply Last reply
            0
            • Y Yusuf

              why are you populating comboBox3 from db when something got selected on the same combobox? You know showing any other thing except populating your combobox. You need to do this only once or when ever the data changes.

              Yusuf

              H Offline
              H Offline
              haroon1980
              wrote on last edited by
              #6

              thanks Yusuf, i just want to see the product names in this comboBox thats all..and which will come from Products Table... regards, :confused:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups