In this article I will explain with an example, how to fill the ComboBox from Database in Windows Forms using C# and VB.Net.
 
 
Database
I have made use of the following table Customers with the schema as follows.
Fill ComboBox from Database in Windows Application using C# and VB.Net
 
I have already inserted few records in the table.
Fill ComboBox from Database in Windows Application using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
Form Design
The Form consists of a ComboBox control and a Button. The Button has been assigned Click event handler.
Fill ComboBox from Database in Windows Application using C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Fill ComboBox from Database using C# and VB.Net
Inside the Form Load event, the records from the database are fetched into a DataTable.
Once the DataTable is populated from database, a new row is inserted in First position which will act as the Default (Blank) item for the ComboBox.
C#
private void Form1_Load(object sender, EventArgs e)
{
    string constr = @"Data Source=.\SQL2014;Initial Catalog=AjaxSamples;Integrated Security=true";
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter("SELECT CustomerId, Name FROM Customers", con))
        {
            //Fill the DataTable with records from Table.
            DataTable dt = new DataTable();
            sda.Fill(dt);
 
            //Insert the Default Item to DataTable.
            DataRow row = dt.NewRow();
            row[0] = 0;
            row[1] = "Please select";
            dt.Rows.InsertAt(row, 0);
 
            //Assign DataTable as DataSource.
            cbCustomers.DataSource = dt;
            cbCustomers.DisplayMember = "Name";
            cbCustomers.ValueMember = "CustomerId";
        }
    }
}
 
VB.Net
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    Dim constr As String = "Data Source=.\SQL2014;Initial Catalog=AjaxSamples;Integrated Security=true"
    Using con As SqlConnection = New SqlConnection(constr)
        Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerId, Name FROM Customers", con)
            'Fill the DataTable with records from Table.
            Dim dt As DataTable = New DataTable()
            sda.Fill(dt)
 
            'Insert the Default Item to DataTable.
            Dim row As DataRow = dt.NewRow()
            row(0) = 0
            row(1) = "Please select"
            dt.Rows.InsertAt(row, 0)
 
            'Assign DataTable as DataSource.
            cbCustomers.DataSource = dt
            cbCustomers.DisplayMember = "Name"
            cbCustomers.ValueMember = "CustomerId"
        End Using
    End Using
End Sub
 
 
Fetching the selected Text and Value of ComboBox
When the Button is clicked, the Selected Text and Value of the ComboBox is fetched and displayed using MessageBox.
C#
private void btnSubmit_Click(object sender, EventArgs e)
{
    string message = "Name: " + cbCustomers.Text;
    message += Environment.NewLine;
    message += "CustomerId: " + cbCustomers.SelectedValue;
    MessageBox.Show(message);
}
 
VB.Net
Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
    Dim message As String = "Name: " & cbCustomers.Text
    message += Environment.NewLine
    message += "CustomerId: " & cbCustomers.SelectedValue
    MessageBox.Show(message)
End Sub
 
 
Screenshot
Fill ComboBox from Database in Windows Application using C# and VB.Net
 
 
Downloads