Autocomplete TextBox in Windows Form Application using SQL Server

Introduction

In this blog we Lear how to create autocompelete textbox on the basis of SQL Server data.

Step 1:

First open Visual Studio ->File->New->Project->Windows From Application->Ok

Drag and Drop label and TextBox.

AutoComplete TextBox

Step 2:

Now Double Click on Windows Form and write the following Code :

SqlConnection con = new SqlConnection(@"Data Source=IS9;Initial Catalog=practice;User ID=sa;Password=abc");

public void loadData()

{

    con.Open();

    AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();

    string querry = @"Select distinct [name] from [bio]";

    SqlCommand cmd = new SqlCommand(querry, con);

    SqlDataReader dr = cmd.ExecuteReader();

    if (dr.HasRows == true)

    {

    while (dr.Read())

    namesCollection.Add(dr["name"].ToString());

 

    }

 

    dr.Close();

    con.Close();

 

    textBox1.AutoCompleteMode = AutoCompleteMode.Append;

    textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

    textBox1.AutoCompleteCustomSource = namesCollection;

}

 

private void textBox1_KeyUp(object sender, KeyEventArgs e)

{

    loadData();

}

Output:

Press F5

Automatic Value in TextBox