How to display data into combobox from database using csharp

03/06/2017   02:30:24 PM

Hello,in this C# tutorial i will explain how to display data from  sql server database to comboboxwe we will use DataTable object,fetch the data from database to DataTable and then assign this DataTable object to ComboBox for display data in to ComboBox,in this tutorial we will display employee name list in to the ComboBox.

1 .Create a database name as Employee.Create a Table name as EmployeeDetails in this database.Create the following fields in to table EmployeeDetails
Column Name
       Int [Identity Property Set =True]


2. Open / Select C# / Windows Application / Rename Form1.cs as EmployeeDetails.


3. For add a combobox control on form go to view / toolbox / select and drag combobox on form


4.Add the following code in to App.config file in </configuration> tag

<add name="cn" connectionString="Data Source=.\sqlexpress;Initial Catalog=Employee;Integrated Security=True"/>


5.Add a refrence System.Configurtation

Right click on refrences folder in project folder/Add Refernces / From Refrence Manager window/ Assemblies option / click on Framework option / select System.Configuration from list /click on ok

Now System.Configuration added in project


6.Create a class dbcon.cs in project and add the following code in that class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace Comboboxdatafromdb
class dbcon
public static string dbconnectivity()
return ConfigurationManager.ConnectionStrings["cn"].ConnectionString;


7.On Form_Load event add the following code for display data in combobox.
For genrate form_load event simply double click on form EmployeeDetails[Design].cs
then EmployeeDetails_Load will genrate

using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace Comboboxdatafromdb
    public partial class Form1 : Form
        public Form1()
        SqlConnection cn = new SqlConnection(dbConnect.dbconnectivity());
        SqlDataAdapter da;
        DataTable dt;
        private void Form1_Load(object sender, EventArgs e)
            da = new SqlDataAdapter("Select * From EmployeeDetails",cn);
            dt = new DataTable();
            DataRow dr = dt.NewRow();
            dr["EmpName"] = "Select Employee Name";
            dt.Rows.InsertAt(dr, 0);
            cmbEmpName.ValueMember = "EmpId";
            cmbEmpName.DisplayMember = "EmpName";
            cmbEmpName.DataSource = dt;




Support us by Sharing