Filling Data in the Combo Box Using C#

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.

FillCBOtwoDisplayFig.3

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.

FillCBOtwoDisplayFig.1

Step 2. Go to the Solution Explorer, hit the “View Code”  to fire the code editor.

FillCBOtwoDisplayFig.2

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:

FillCBOtwoDisplayFig.3

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

Download Sourcecode

ABOUT PROJECTPROJECT 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

Leave a Comment