News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Nov 30 2014 8:11AM     Prashanth
  14 Comments    24423 Views  
Here Prashanth provided a short tutorial with example, how to fill Combobox Items from Database in Windows Forms Application in C#.
Database
For filling the ComboBox, First have to create a table in Microsoft SQL Server Database as shown below.
Creating a table
When creating a table make Identity Specification as true for column 'ID'.

Set the table name as shown below.
Adding a ComboBox Control to the Windows Form
Firstly you need to add a ComboBox control to the Windows Form from the Visual Studio ToolBox as shown below.

Form Design
Namespaces
You will need to import the following namespace.

using System.Data.SqlClient;

Adding Items to ComboBox from Database in C#
Below is the sample code for Adding Items to ComboBox from Database.
C#

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FillCombobox();
        }

        protected void FillCombobox()
        {
            string conString = @"server=YOUR SERVERNAME; database=YOUR DATABASE NAME; uid=***; password=*****;";
            SqlConnection conn = new SqlConnection(conString);
            DataSet ds = new DataSet();
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select ID,EmployeeName from Employee group by ID, EmployeeName", conn);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds);              
                comboBox1.DisplayMember = "EmployeeName";
                comboBox1.ValueMember = "ID";
                comboBox1.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                //Exception Message
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }
    }

Result
I hope this page will helps to fill ComboBox Items in Windows Forms Application in C#. Thanks.
BackToTop
Comments


sepehr shakeri
Feb 29 2016 8:00AM
how to 2 or more items in combo box ? for eg mix of three column
Arun Kumar
Feb 29 2016 9:25AM
Hi Sepehr Shakeri, you mean three columns from database.

For Example:

In table, three columns like Country1, Country2, Country3.
In Combobox you have to fill with all the three column content.

Eg: Country1 + Country2 + Country3
AnonymousUser
Jul 27 2016 3:22AM
good
AnonymousUser
Aug 7 2016 12:21PM
how to call a parameterized method to form load
Arun Kumar
Aug 10 2016 10:20PM
You just want to call method with parameter on form load,

Eg:

In Form Load :
FillCombobox("TestMessage");

In Method :
protected void FillCombobox(string Message)
{
...
}
AnonymousUser
Jan 20 2017 11:26AM
how to add items on combobox of one table from another table
AnonymousUser
Jan 20 2017 11:26AM
how to add items on combobox of one table from another table
AnonymousUser
Mar 6 2017 10:41PM
Good
AnonymousUser
Jan 11 2018 10:52PM
if i am select combobox value then to fetch that table values and display in some textboxes so how to write code
Arun Kumar
Mar 10 2018 9:34AM
Hi, For Eg. First you get the Employee Id by Using "Combobox1.SelectedValue" then you will be getting employee Id for the selected employee.

Then pass that employee id to fetch the employee details... Thanks...
AnonymousUser
Mar 5 2018 8:30AM
welcome
AnonymousUser
Jun 19 2018 10:38AM
how we can pass these combobox value in parameter--In Inserting form .
Vishwanath
Jun 29 2018 10:02AM
You can select the value by "Combobox1.SelectedValue", then you can pass this to any methods with the type of integer. Thanks..
AnonymousUser
Feb 25 2019 1:19AM
simple and quick painless

 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Oct 29 2018 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Oct 28 2018 12:09PM Posted By Pranav
Google URL Shortener in C#
Oct 20 2018 12:09PM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Oct 5 2018 12:09PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Sep 26 2018 12:09PM Posted By John
Gridview custom CSS in ASP.Net
Sep 14 2018 12:09PM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Sep 10 2018 12:09PM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: