Filling Data in the Combo Box with Two Display Members Using C# and SQL Server
In this tutorial, I will teach you how to fill the data in the combo box with two display members using C#.net and SQL Server 2005 express edition.
This method has the capability to display two columns in a combo box from the table of the database. Using concatenation, this guarantees you that it will work because concatenation is a procedure that joins two or more fields in the table of the database.
Here’s the link for creating a database. Click Here
Let’s begin:
Step 1. Open Visual Studio and create new Windows Form Application. After that, drag a comboBox in the Form.
Step 2. Go to the Solution Explorer, hit the “View Code” to fire the code editor.
Step 3. Declare and initialize the classes that are needed.
Note: Put “using System.Data.SqlClient;” above the namespace to access sql server library.
//initialize all classes SqlConnection conn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); SqlDataAdapter da = new SqlDataAdapter(); DataTable dt = new DataTable();
Step 4. Create a method for filling data in the comboBox with two display member.
private void FillCBO()
{
//opening connection
conn.Open();
try
{
//set a query for combining two fields in the table
string sqlQuery = "Select ID, (NAME + ' ,' + AGE ) as 'NameAge' FROM tblinfo";
//initialize a new instance of sqlcommand
cmd = new SqlCommand();
//set a connection used by this instance of sqlcommand
cmd.Connection = conn;
//set the sql statement to execute at the data source
cmd.CommandText = sqlQuery;
//initialize a new instance of sqlDataAdapter
da = new SqlDataAdapter();
//set the sql statement or stored procedure to execute at the data source
da.SelectCommand = cmd;
//initialize a new instance of DataTable
dt = new DataTable();
//add or resfresh rows in the certain range in the datatable to match those in the data source.
da.Fill(dt);
//Get and set the data source of a comboBox
comboBox1.DataSource = dt;
//set the field of a table to display in the list control of a combo box
comboBox1.DisplayMember = "NameAge";
//Display first in the list control
comboBox1.Text = "Select";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message );
}
da.Dispose();
conn.Close();
}Step 5. Set up the connection between SQL Server database and C#.net and call the method that you have created on the first load of the form.
private void Form1_Load(object sender, EventArgs e)
{
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Database=dbinfo;trusted_connection=true;";
//call a method to display the data in the combobox in the first load of the form
FillCBO();
}Output:
For all students who need a programmer for your thesis system or anyone who needs a source code in any programming languages. You can contact me @ :
Email – [email protected]
Mobile No. – 09305235027 – talk’nText
| ABOUT PROJECT | PROJECT DETAILS |
|---|---|
| Project Name : | Filling Data in the Combo Box with Two Display Members Using C# and SQL Server |
| Project Platform : | C# |
| Programming Language Used: | C# Programming Language |
| Developer Name : | itsourcecode.com |
| IDE Tool (Recommended): | Visual Studio 2019 |
| Project Type : | Desktop Application |
| Database: | None |
| Upload Date and Time: | July 23, 2016 – 9:37 am |
Frequently Asked Questions
How does this C# ComboBox example work?
Populating ComboBox items (from array, List
What .NET and SQL Server versions does this project require?
Most projects in this batch use C# WinForms on .NET Framework 4.5+ (the dominant stack for tutorial sites) with SQL Server 2012 Express or higher. A few newer projects use .NET 6/7/8. To run: install Visual Studio 2019 / 2022 (Community edition is free), install SQL Server Express + SSMS, open the .sln file, build, run.
How do I set up the database for this C# project?
Open SQL Server Management Studio (SSMS) and connect to your SQL Server instance (e.g. localhost\SQLEXPRESS). Right-click Databases > Restore Database OR > New Database then import the included .sql script. Update the connection string in App.config (or in code-behind) with your server name + credentials. Rebuild and run.
Can I use this C# project for a BSIT capstone or thesis?
Yes, but extend it. A bare CRUD form is too narrow for full capstone scope. Add: role-based access (admin/staff/customer login redirect), Crystal Reports or RDLC reports, dashboard with Chart controls, audit log, multi-branch support. Pair with Chapter 1-5 documentation matching your panel’s rubric.
Why am I getting ‘connection error’ or ‘object reference not set’?
Three common C# issues: (1) Connection error: SQL Server isn’t running OR connection string in App.config has wrong server name. Open SQL Server Configuration Manager + verify SQL Server (SQLEXPRESS) service is running. (2) NullReferenceException: a control reference or DB column returned NULL, add a check or use ?? operator. (3) Build error ‘The type or namespace could not be found’: missing assembly reference, add via Project > Add Reference.
Where can I find more C# projects with source code?
Browse the C# Projects hub for the full library. For other .NET stacks see VB.NET Projects (300+ Windows Forms systems). For ASP.NET WebForms see ASP.NET Projects. For BSIT capstone idea lists see 150 Best Capstone Project Ideas.


