Free Source Code and Tutorials

Custom Search

Autocomplete in a ComboBox Using C# and SQL Server

In this tutorial, I will teach you how to create an Autocomple in a Combobox using and SQL Server 2005. With this, it will suggest the records when you start to type in the combobox then, you don’t have to click anymore the dropdown or scroll down the list of records that you’re going to select.


Let’s begin:


Create a database and name it “dbcombo”.

After creating database, do the following query for creating a table in the database that you have created.

/****** Object: Table [dbo].[tblplaces] Script Date: 06/19/2016 15:40:21 ******/
CREATE TABLE [dbo].[tblplaces](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [Places] [nvarchar](50) NULL

Open Microsoft Visual Studio 2008 and create new Windows Form Application for C#. Then do the following design of a Form just like this.


Go to the Solution Explorer, click the “View Code”  to view the code editor.


In the code editor, declare all the classes that are needed.

Note: Put using System.Data.SqlClient; above the namespace to access sql server library.

 //initialize all classes
 //initializes new instance 
 SqlConnection con = new SqlConnection();
 SqlDataAdapter da = new SqlDataAdapter();
 SqlCommand cmd = new SqlCommand(); 
 DataTable dt = new DataTable();

After declaring the classes, create a method for retrieving data in the database with autocomplete in a  ComboBox.

 private void AutoCombo(string query, ComboBox cbo)
 //opening connection
 //initialize a new instance of sqlcommand
 cmd = new SqlCommand();
 //set a connection used by this instance of sqlcommand
 cmd.Connection = con;
 //set the sql statement to execute at the data source
 cmd.CommandText = query;

 //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.
 foreach (DataRow r in dt.Rows)
 //getting all rows in the specific field|Column
 var rw = r.Field<string>("Places");

 //Set the properties of a combobox to make it auto suggest.
 cbo.AutoCompleteMode = AutoCompleteMode.Suggest;
 cbo.AutoCompleteSource = AutoCompleteSource.CustomSource;
 //adding all rows into the combobox


 catch (Exception ex)
 //catching error 
 //release all resources used by the component
 //clossing connection


Finally, go back to the design view double click the form and create a connection between SQL server and Then, call the method that you have created.

private void Form1_Load(object sender, EventArgs e)
 //set a connection between SQL server and Visual C#
 con.ConnectionString = "Data Source=.\\SQLEXPRESS;Database=dbcombo;trusted_connection=true;";
//create a query for retrieving data in the database
 string query = "Select * From tblplaces";
 AutoCombo (query ,comboBox1 ); //call a method that you have created.





For all students who need programmer for your thesis system or anyone who needs a sourcecode in any programming languages. You can contact me @ :
Email –
Mobile No. – 09305235027 – tnt


Facebook Comments
(Visited 42 times, 1 visits today)
Share this

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked * © 2016 Frontier Theme

Subscribe For Latest Updates

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

%d bloggers like this: