ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Filling Data in the ComboBox with Two Display Members Using C# and SQL Server

FillCBOtwoDisplayFig.3In 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 2008 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 – jannopalacios@gmail.com
Mobile No. – 09305235027 – talk’nText

 

Download Sourcecode

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!