Databinding in Combobox using C#

In this blog we will know how to bind data from the database to a combobox control in a windows application. Here we will know in three ways how to bind data to a combobox. Out of three methods, we use DataAdapter in two methods and DataReader in other one.
 
Table structure
 
Create a table named as student in ms-access having column names as below
sid text,sname text,smarks number,saddress text,year text
 
Create table in sql

Add an App.config file to the application
 
 
<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
 <appSettings>
 <add key="dsn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\EMP.mdb" />
 </appSettings>
 </configuration>
 
 
 
using
System;
 
using System.Collections.Generic;
 
using System.ComponentModel;
 
using System.Data;
 
using System.Drawing;
 
using System.Linq;
 
using System.Text;
 
using System.Windows.Forms;
 
using System.Data.OleDb;
 
namespace Dynamically_bind_data_to_combobox_from_database
 {
     public partial class Form1 : Form
 
    {
         string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
         OleDbCommand com;
         OleDbDataAdapter oledbda;
         DataSet ds;
         string str;
         DataTable dt;
         public Form1()
         {
             InitializeComponent();
         }
  
         private void button1_Click(object sender, EventArgs e)
         {           
             OleDbConnection con = new OleDbConnection(ConnectionString);
             con.Open();
             str = "select * from student";
             com = new OleDbCommand(str, con);
             oledbda = new OleDbDataAdapter(com);
             ds = new DataSet();
             oledbda.Fill(ds, "student");
             dt = ds.Tables["student"];
             int i;
             for (i = 0; i <= dt.Rows.Count - 1; i++)
             {
                 comboBox1.Items.Add(dt.Rows[i].ItemArray[0]);
             }
             con.Close();
         }
  
         private void button2_Click(object sender, EventArgs e)
         {
             OleDbConnection con = new OleDbConnection(ConnectionString);
             con.Open();
             str = "select * from student";
             com = new OleDbCommand(str, con);
             oledbda = new OleDbDataAdapter(com);
             ds = new DataSet();
             oledbda.Fill(ds, "student");
             comboBox2.DataSource = ds.Tables["student"];
             comboBox2.DisplayMember = "sname";
             con.Close();
         }
  
         private void button3_Click(object sender, EventArgs e)
         {           
             OleDbConnection con = new OleDbConnection(ConnectionString);
             con.Open();
             str = "select * from student";
             com = new OleDbCommand(str, con);
             OleDbDataReader reader = com.ExecuteReader();
             while (reader.Read())
             {
                 comboBox3.Items.Add(reader["smarks"]);
             }
             reader.Close();
             con.Close();
         }
  
         private void Form1_Load(object sender, EventArgs e)
         {
             comboBox1.Text="Plz Click the button";
             comboBox2.Text="Plz Click the button";
             comboBox3.Text="Plz Click the button";
         }
     }
 }
 

Output
 
databindg in c#.gif

Thanks for reading