How to Find Records Using an Autocomplete TextBox in C#.

In this tutorial, I will teach you how to find records using an autocomplete textbox in c#. This method has the ability to find records immediately by typing the data in the textbox . It also has the capability to auto-suggest and appends if there is an existing name in the database that you type in the textbox. Lets begin.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in C#. ps1

Step 2

Do the form just like shown below. ps2

Step 3

Press F7 to open the code editor. In the code editor, add a namespace for OLeDB to access OLeDB libraries.
  1. using System.Data.OleDb;

Step 4

Create a connection between access database and c#. After that, declare all the classes that are needed.
  1.         OleDbConnection con = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + Application.StartupPath + "/studentdb.accdb");
  2.         OleDbCommand cmd;
  3.         OleDbDataAdapter da;
  4.         DataTable dt;
  5.         string sql;

Step 5

Create a method for autocomplete textbox.
  1.  
  2.         private void autocomplete_texbox(string sql,TextBox txt)
  3.         {
  4.             try
  5.             {
  6.                 con.Open();
  7.  
  8.                 cmd = new OleDbCommand();
  9.                 da = new OleDbDataAdapter();
  10.                 dt = new DataTable();
  11.  
  12.                 cmd.Connection = con;
  13.                 cmd.CommandText = sql;
  14.  
  15.                 da.SelectCommand = cmd;
  16.                 da.Fill(dt);
  17.                 txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
  18.                 txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
  19.                 txt.AutoCompleteCustomSource.Clear();
  20.  
  21.                 foreach(DataRow r in dt.Rows)
  22.                 {
  23.                     txt.AutoCompleteCustomSource.Add(r.Field<string>("Fname"));
  24.                 }
  25.  
  26.             }
  27.             catch(Exception ex)
  28.             {
  29.                 MessageBox.Show(ex.Message);
  30.             }
  31.             finally
  32.             {
  33.                 con.Close();
  34.                 da.Dispose();
  35.             }
  36.         }

Step 6

Create a method for searching data.
  1.  
  2.         private void find_Records(string sql,DataGridView dtg)
  3.         {
  4.             try
  5.             {
  6.                 con.Open();
  7.  
  8.                 cmd = new OleDbCommand();
  9.                 da = new OleDbDataAdapter();
  10.                 dt = new DataTable();
  11.  
  12.                 cmd.Connection = con;
  13.                 cmd.CommandText = sql;
  14.  
  15.                 da.SelectCommand = cmd;
  16.                 da.Fill(dt);
  17.  
  18.                 dtg.DataSource = dt;
  19.  
  20.             }
  21.             catch (Exception ex)
  22.             {
  23.                 MessageBox.Show(ex.Message);
  24.             }
  25.             finally
  26.             {
  27.                 con.Close();
  28.                 da.Dispose();
  29.             }
  30.  
  31.         }

Step 7

Do the following code to execute the autocomplete in a textbox method in the first load of the form.
  1.  
  2.         private void Form1_Load(object sender, EventArgs e)
  3.         {
  4.             sql = "Select * From tblstudent";
  5.             autocomplete_texbox(sql, textBox1);
  6.  
  7.         }

Step 8

Do the following code to execute the searching of data method.
  1.  
  2.         private void textBox1_TextChanged(object sender, EventArgs e)
  3.         {
  4.             sql = "Select * FROM tblstudent WHERE Fname Like '%" + textBox1.Text + "%'";
  5.             find_Records(sql, dataGridView1);
  6.         }
Download the complete source code and run it on your computer. For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below

Add new comment