Autocomplete Textbox from database in windows forms Application

In this article I will explain you how to implement autocomplete textbox from database in windows forms Application in asp.net.
first we need to create database for implement autocomplete textbox in windows application.
Here, we create database in sql server, database name is :AutoComplete

Create a Table in Database, Table name is : UserMst
Design table with 1 column named UserName.

UserName as nvarchar(50) datatype Allow null = Yes

After completion of design table enter some values in table manually for our example.

Now, design a windows for with one TextBox control ID = txtName

First set two main TextBox control property for autocomplete.

autocomplete textbox

autocomplete textbox from database in windows forms application

AutoCompleteMode = Suggest , Append or SuggestAppend
Set anyone from three of above for autocomplete textbox

AutoComlateSource = CustomSource

autocomplete textbox

autocomplete textbox from database in windows forms application

After doing this write below code at Form_Load event of windows form

private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(“Data Source=’.\\SQLEXPRESS’;Integrated Security=’true’;Initial Catalog=’AutoComplete’”);
SqlCommand cmd = new SqlCommand(“SELECT UserName FROM UserMst”, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
AutoCompleteStringCollection SCollection = new AutoCompleteStringCollection();
while (reader.Read())
{
SCollection.Add(reader.GetString(0));
}
txtName.AutoCompleteCustomSource = SCollection;
con.Close();
}

autocomplete textbox

autocomplete textbox from database in windows forms application

autocomplete textbox

autocomplete textbox from database in windows forms application

After write above code in load event just run your windows form and enjoy autocomplete textbox from database in windows application.

Leave a Reply

Your email address will not be published. Required fields are marked *

6 + 4 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">