ComboBox C#

In this tutorial we will learn how to create combobox in windows form and fill the combobox using database table

Let's create windows form.

Step 1
In Visual Studio select "File" = > "New" = > "Project..." then select C# Windows Forms Application then click Ok.

create windows form

Step 2
Let's create a ComboBox then you go "View" = > select "ToolBox" (Crtl+Alt+X) = > then select "All Windows Forms" = > Choose "ComboBox".

create textbox control

Store procedure :

create procedure sp_getting_tblLogin
select * from tblLogin

Application configuration file(App.config) :

If you want to create "App.config" file then you press this shortcut key "Crtl+Shift+A" then you choose the Applicatiion configuration file(App.config)

<?xml version="1.0" encoding="utf-8" ?>
<add name="DBCS" connectionString="data source=.;database=dbSample;integrated security=SSPI" providerName="System.Data.SqlClient"/>

Note :-

  • data source: you enter your own sql server name(if you use local server then you use(.))
  • database : you enter your own database name
  • integrated security=SSPI means this is use windows authentication. If you use sql server authentication then you use the (Username=_yourusername;Password=_youpassword) then you replace in intergrated security portion.
  • Add the System.Configuration reference. if you don't know how to adding references right click your project folder references => add reference => select .Net => select System.Configuration => click OK

Form1.cs file :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace Testing
    public partial class Form1 : Form
        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            //create a list to store the "GettingFromDatabase()" function return value
            List <string> name = GettingFromDatabase();
            foreach loop continues name list
            foreach (string i in name)
               //Adding value in combobox items
            //First time show comboxbox index are 0
            comboBox1.SelectedIndex = 0;
        private List<string> GettingFromDatabase()
            //Create a string list
            List<string> name=new List<string>();
            //getting connection string into "App.config" file
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            //send the connection string in "SqlConnection" class
            SqlConnection con = new SqlConnection(cs);
            //send the "commandtext=storeProcedure name" and connection object
            SqlCommand cmd = new SqlCommand("sp_getting_tblName",con);
            //Specify the command type is store procedure
            cmd.CommandType = CommandType.StoredProcedure;
                //Open the connection
                //"SqlDataReader" store the value which is returned by "cmd.ExecuteReader()"
                SqlDataReader sdr = cmd.ExecuteReader();
                //sdr read the data in looping
                while (sdr.Read())
                    //name list adding data which is supplied "sdr["Name"]"(it means database table Name column all data are adding in list)
                //Destroy the resources which are captured by "cmd"
            Any exception are occured then show the message box
            catch (Exception ex)
            //"con" destroy all resources and connection closed
            //return the name list
            return name;



Write a comment

Name Email Message