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
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()
//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.
//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)
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
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