Simple Search Based on Two Columns in C# and MySQL Database

In this tutorial, I will teach you how to create a simple search based on two columns in c# and MySQL database. This program will illustrate how to search for data or records in the database based on two fields in the table. It has an automatic search for data in the database that will be displayed into the datagridview. Let’s begin.

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

Open the code editor by pressing the F7 on the keyboard. In the code editor, add a namespace to access MySQL Libraries.
  1. using MySql.Data.MySqlClient;

Step 4

Create a connection between C# and MySQL Database. After that, declare all the classes that are needed.
  1.         MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=dbsubjects;sslMode=none");
  2.         MySqlCommand cmd;
  3.         MySqlDataAdapter da;
  4.         DataTable dt;
  5.         string sql;

Step 5

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

Step 6

Double click the form and do the following codes for retrieving data in the database in the first load of the form.
  1.         private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             sql = "SELECT `SUBJ_ID` as 'ID', `SUBJ_CODE` as 'CODE', `SUBJ_DESCRIPTION` as 'DESCRIPTION', `UNIT`, `PRE_REQUISITE` AS 'PRE-REQUISITE' FROM `subject` WHERE `SUBJ_CODE` LIKE '%" + textBox1.Text + "%' OR `SUBJ_DESCRIPTION` LIKE '%" + textBox1.Text + "%'";
  4.             simple_search(sql, dataGridView1);
  5.         }

Step 7

Write this code for searching data in the database.
  1.         private void textBox1_TextChanged(object sender, EventArgs e)
  2.         {
  3.             sql = "SELECT `SUBJ_ID` as 'ID', `SUBJ_CODE` as 'CODE', `SUBJ_DESCRIPTION` as 'DESCRIPTION', `UNIT`, `PRE_REQUISITE` AS 'PRE-REQUISITE' FROM `subject` WHERE `SUBJ_CODE` LIKE '%" + textBox1 .Text + "%' OR `SUBJ_DESCRIPTION` LIKE '%" + textBox1.Text + "%'";
  4.             simple_search(sql, dataGridView1);
  5.         }
The complete source code is included, yo 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