ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Autocomplete in a ComboBox Using C# and SQL Server

autocompleteCBOSQLfig.3
In this tutorial, I will teach you how to create an Autocomple in a Combobox using C#.net 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 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblplaces](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [Places] [nvarchar](50) NULL
) ON [PRIMARY]

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

autocompleteCBOSQLfig.1

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

autocompleteCBOSQLfig.2

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
 con.Open();
 try
 {
 //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.
 da.Fill(dt);
 
 
 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
 cbo.AutoCompleteCustomSource.Add(rw);

 }

 }
 catch (Exception ex)
 {
 //catching error 
 MessageBox.Show(ex.Message);
 }
 //release all resources used by the component
 da.Dispose(); 
 //clossing connection
 con.Close();

 }

Finally, go back to the design view double click the form and create a connection between SQL server and C#.net. 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.

}

 

Output:

autocompleteCBOSQLfig.3

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 – jannopalacios@gmail.com
Mobile No. – 09305235027 – tnt

 

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!