Click here to Skip to main content
13,195,379 members (62,851 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 12 Apr 2012

Autosuggest TextBox from database column in Windows Forms

, 19 Apr 2012
Rate this:
Please Sign up or sign in to vote.
This article will show how to create an auto-suggest TextBox that will suggest data from a SQL Server database column.


In this article we will create a TextBox that will suggest names from a SQL Server database column FirstName. Typing first few characters will show matching names from the FirstName column as dropdown.

Using the code

Create a new Windows Forms Application and add a TextBox and a Label on Form1.

Set the following properties of the TextBox.

Property NameValue
(Name) txtFirstName
AutoCompleteSource CustomSource
AutoCompleteMode SuggestAppend

AutoCompleteSource property sets the source for auto complete data. It can be set to a AutoCompleteSource enumeration, FileSystem, HistoryList, RecentlyUsedList, AllUrl, AllSystemSources, FileSystemDirectories, CustomSource or None. As we are getting our own data we set it to CustomSource.

AutoCompleteMode property defines how text is suggested in the TextBox. It can be set to a AutoCompleteMode enumeration, App<code>end, Suggest, SuggestAppend, None. Suggest displays all the suggestions as dropdown. Append displays first value of the suggestion appended or selected in the TextBox, other values can be navigated using arrow keys. SuggestAppend displays suggested values as dropdown and first value appended in the TextBox.

Write following code in the Load event of Form1

private void Form1_Load(object sender, EventArgs e)
    string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(ConString))
        SqlCommand cmd = new SqlCommand("SELECT FirstName FROM Employees", con);
        SqlDataReader reader = cmd.ExecuteReader();
        AutoCompleteStringCollection MyCollection = new AutoCompleteStringCollection();
        while (reader.Read())
        txtFirstName.AutoCompleteCustomSource = MyCollection;

Here, first we get we get connection string from App.Config file in ConString variable. Then using SqlDataReader add FirstName values to a AutoCompleteStringCollection object MyCollection. AutoCompleteCustomSource accepts AutoCompleteStringCollection object.

Now when you run the application and type some text in the TextBox, you will get output as in the above figure.


  • 4th April, 2012: First version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
No Biography provided

You may also be interested in...


Comments and Discussions

Questionthanks but.. Pin
dash202015-Mar-14 6:14
memberdash202015-Mar-14 6:14 
QuestionCan't Download Pin
onurag1915-Apr-12 3:43
memberonurag1915-Apr-12 3:43 
Questionwhat is AutoCompleteStringCollection class? Pin
Tridip Bhattacharjee12-Apr-12 20:23
memberTridip Bhattacharjee12-Apr-12 20:23 
AnswerRe: what is AutoCompleteStringCollection class? Pin
Deepak Sharma (dpkshr)12-Apr-12 20:48
memberDeepak Sharma (dpkshr)12-Apr-12 20:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171019.1 | Last Updated 19 Apr 2012
Article Copyright 2012 by Deepak_Sharma_
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid