Contact Form

Name

Email *

Message *

Follow on LinkedIn
Image

22. Adding Search Feature on Product Module



In the previous episode, we have successfully added functionalities to INSERT, UPDATE and DELETE Products in our Product Module for our "BILLING SYSTEM IN C#".
In this episode, we will add a functionality to SEARCH product based on ID and NAME.
Thus, the methods and events needed to add this functionality are as follows
WATCH THE VIDEO FIRST

1. Method to Search Product

In order to search product from database following method is created in productDAL.cs

        public DataTable Search (string keywords)
        {
            //SQL Connection fro DB Connection
            SqlConnection conn = new SqlConnection(myconnstrng);
            //Creating DAtaTable to hold value from dAtabase
            DataTable dt = new DataTable();

            try
            {
                //SQL query to search product
                string sql = "SELECT * FROM tbl_products WHERE id LIKE '%"+keywords+"%' OR name LIKE '%"+keywords+"%' OR category LIKE '%"+keywords+"%'";
                //Sql Command to execute Query
                SqlCommand cmd = new SqlCommand(sql, conn);

                //SQL Data Adapter to hold the data from database temporarily
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);

                //Open Database Connection
                conn.Open();

                adapter.Fill(dt);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }

            return dt;
        }

2. Text Changed Event to Search Product

In order to create text changed event, Select the search box and double click on it.
And the code inside the text changed event is as follows

      private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            //Get the Keywordss from Form
            string keywords = txtSearch.Text;

            if(keywords!=null)
            {
                //Search the products
                DataTable dt = pdal.Search(keywords);
                dgvProducts.DataSource = dt;
            }
            else
            {
                //Display All the products
                DataTable dt = pdal.Select();
                dgvProducts.DataSource = dt;
            }
        }

Thank you so much for reading and watching my tutorial. If you want to see more posts like this, then don't forget to SUBSCRIBE.

If you want to START from the beginning of this course, then CLICK HERE.

BILLING AND INVENTORY MANAGEMENT SYSTEM IN C#

Comments