Contact Form

Name

Email *

Message *

Follow on LinkedIn
Image

8. Adding Search Feature on User Module



This is the 8th episode of my tutorial series on "BILLING SYSTEM in C# and MS SQL SERVER". In this episode, we will be adding actual functionality to search Users based on their id, first name, last name and username, and then show the results on our data grid view.

In order to add this SEARCH feature, we need to create new method on our userDAL.cs and a text changed event on our frmUSER. If your are new, then you might not know what userDAL.cs file is, it is a Data Access Layer (DAL) class containing all the methods to Add new user, Update Existing User, Display All Users and Delete Users.

So, if you are interested in this course then CLICK HERE to join.

Method to add Search feature on our user module

Copy the code and paste it in userDAL.cs under delete method. I highly recommend to watch the tutorial first.

#region Search User on Database usingKeywords
        public DataTable Search(string keywords)
        {
            //Static MEthod to connect Database
            SqlConnection conn = new SqlConnection(myconnstrng);
            //TO hold the data from database
            DataTable dt = new DataTable();
            try
            {
                //SQL Query to Get Data From DAtabase
                String sql = "SELECT * FROM tbl_users WHERE id LIKE '%"+keywords+"%' OR first_name LIKE '%"+keywords+"%' OR last_name LIKE '%"+keywords+"%' OR username LIKE '%"+keywords+"%'";
                //For Executing Command
                SqlCommand cmd = new SqlCommand(sql, conn);
                //Getting DAta from dAtabase
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                //Database Connection Open
                conn.Open();
                //Fill Data in our DataTable
                adapter.Fill(dt);
            }
            catch (Exception ex)
            {
                //Throw Message if any error occurs
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //Closing Connection
                conn.Close();
            }
            //Return the value in DataTable
            return dt;
        }
        #endregion

That's it. If you do not have any error, then you have successfully created the method to search user in User Module.

Now, Next Step is to create text changed event in our USER form i.e. frmUsers.cs.

Text Changed Event

Steps to create textchanged event

  1. Open frmUsers.cs file from UI folder.
  2. Then select search textbox.
  3. Now, go to properties and click on thunder icon (Event) and search for TextChanged and double click on that event
  4. Now you will see code view of that form and TextChanged event will be created.
  5. Now, add the following code inside the TextChanged Event

//Get Keyword from Text box

            string keywords = txtSearch.Text;

            //Chec if the keywords has value or not
            if(keywords!=null)
            {
                //Show user based on keywords
                DataTable dt = dal.Search(keywords);
                dgvUsers.DataSource = dt;
            }
            else
            {
                //show all users from the database
                DataTable dt = dal.Select();
                dgvUsers.DataSource = dt;
            }


If you are following my tutorial series, then this code will work perfectly But if you have changed the variable names then should make change on this code accordingly.

Thank you so much for reading my post.

If you are new and is interested to join this course please click on the link below

Comments