The Easy Way to Search Data in ListView Using C# and MS Access Database

In this tutorial, I will teach you how to search for data in the listview with ease by using MS Access database and C#. This method is the easiest way to retrieve data in the database and display it into the listview. It also has an automatic searching of data in the listview. This technique is very useful for you when you are a beginner in this field.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for 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 to access OleDB libraries
  1. using System.Data.OleDb;

Step 4

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

Step 5

Create a method for retrieving data in the database.
  1.         private void load_data(string sql,ListView lst)
  2.         {
  3.             try
  4.             {
  5.                  
  6.  
  7.                 con.Open();
  8.  
  9.                 cmd = new OleDbCommand();
  10.                 da = new OleDbDataAdapter();
  11.                 dt = new DataTable();
  12.  
  13.                 cmd.Connection = con;
  14.                 cmd.CommandText = sql;
  15.                 da.SelectCommand = cmd;
  16.                 da.Fill(dt);
  17.  
  18.                 lst.Items.Clear();
  19.                 foreach (DataRow r in dt.Rows)
  20.                 {
  21.                     var list = lst.Items.Add(r.Field<string>(1).ToString());
  22.                     list.SubItems.Add(r.Field<string>(2).ToString());
  23.                     list.SubItems.Add(r.Field<string>(3).ToString());
  24.                 }
  25.  
  26.  
  27.  
  28.             }
  29.             catch (Exception ex)
  30.             {
  31.                 MessageBox.Show(ex.Message);
  32.             }
  33.             finally
  34.             {
  35.                 con.Close();
  36.                 da.Dispose();
  37.             }
  38.         }

Step 6

Write the following codes to display the data into the ListView in the first load of the form.
  1.         private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.  
  4.             sql = "SELECT * FROM tblperson";
  5.             load_data(sql, listView1);
  6.         }

Step 7

Write the following codes for searching data in the ListView.
  1.         private void textBox1_TextChanged(object sender, EventArgs e)
  2.         {
  3.             sql = "SELECT * FROM tblperson WHERE Name LIKE '%" + textBox1.Text + "%'";
  4.             load_data(sql, listView1);
  5.         }
The complete source code is included you can download it 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