Bind Combo Box With Data Base In C# Windows Desktop Application

Hello friends. In this article I will tell you how you can retrieve database column value in the combo box of C# Windows desktop applications. So let's start .

Step 1
 
Open your Visual Studio and create a new project from File Menu > New > Project
 
Save Image In Row Format / Bytes Into Database And Fetch From Database In C# Windows Desktop Application
 
Step 2
 
Select Windows desktop app from menu:
 
Save Image In Row Format / Bytes Into Database And Fetch From Database In C# Windows Desktop Application
 
Step 3
 
Enter your project name and path of your location where you want to save your project.
 
Save Image In Row Format / Bytes Into Database And Fetch From Database In C# Windows Desktop Application
 
Step 4
 
Create design as you want. As you see in the below image here I only added label and a combo box which is empty.
 
Save Image In Row Format / Bytes Into Database And Fetch From Database In C# Windows Desktop Application
 
Step 5
 
Now add database file. Click on your project name from Solution Explorer Add > New file or you can simply use short cut key Ctrl+Shift+A
 
Save Image In Row Format / Bytes Into Database And Fetch From Database In C# Windows Desktop Application
 
Step 6
 
Select service based database. Give name as you want and click on add.
 
Save Image In Row Format / Bytes Into Database And Fetch From Database In C# Windows Desktop Application
 
Step 7
 
Now create a new table. Double click on your database file; it will open Server Explorer. Then right click on Table > Add New Table . As you see in the below image our table has only two columns,  Id And Name .
 
Save Image In Row Format / Bytes Into Database And Fetch From Database In C# Windows Desktop Application
 
Here is the data of our table,
 
Save Image In Row Format / Bytes Into Database And Fetch From Database In C# Windows Desktop Application
 
Step 8
 
Now copy your connection string by right clicking on Database Name > Modify Connection > Advance .
 
Save Image In Row Format / Bytes Into Database And Fetch From Database In C# Windows Desktop Application 
 
Step 9
 
Now add new SQL Connection on your form load event as shown in the below image .
 
Save Image In Row Format / Bytes Into Database And Fetch From Database In C# Windows Desktop Application
 
Step 10
 
Here I created a separate function for getting data from database and bound it in combo box and I called that function in form load event. You can also write that code directly on form load event.
 
Save Image In Row Format / Bytes Into Database And Fetch From Database In C# Windows Desktop Application
 
Step 11
 
Now run your application. As you can see that combo box gets all the names from the database.
 
Save Image In Row Format / Bytes Into Database And Fetch From Database In C# Windows Desktop Application
 
Below is the full source code of this project,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Data.SqlClient;   
  11. namespace SaveAndDisplayImageFromDatabase  
  12. {  
  13.     public partial class BindComboBox : Form  
  14.     {  
  15.         public BindComboBox()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         SqlConnection cn;  
  20.         SqlCommand cmd;  
  21.         SqlDataReader dr;  
  22.         private void BindComboBox_Load(object sender, EventArgs e)  
  23.         {  
  24.             cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=G:\Tutorial\SaveAndDisplayImageFromDatabase\SaveAndDisplayImageFromDatabase\Database1.mdf;Integrated Security=True");  
  25.             cn.Open();  
  26.   
  27.             BindData();  
  28.         }  
  29.         public void BindData()  
  30.         {  
  31.             cmd = new SqlCommand("select name from Table1", cn);  
  32.             dr = cmd.ExecuteReader();  
  33.             while(dr.Read())  
  34.             {  
  35.                 comboBox1.Items.Add(dr[0].ToString());  
  36.             }  
  37.             dr.Close();  
  38.         }  
  39.   
  40.     }  

I hope you find this article helpful. If you learned anything from this article kindly share with your friends. Thank you.


Recommended Free Ebook
Similar Articles