Billing System
16. Adding Search Functionality in Category Module
In the previous episode, we have successfully added functionality to add new category, update and delete existing category in our Category Module for our "BILLING AND INVENTORY MANAGEMENT SYSTEM IN C#".
In this episode, we will add SEARCH Feature in our Category Module, where we will be searching category based on id, title and description of the categories.
Thus, the Method and Event required to add this feature is shown Below
WATCH THE VIDEO FIRST
1. Search Method for Category Module
public DataTable Search(string keywords)
{
//SQL Connection For Database Connection
SqlConnection conn = new SqlConnection(myconnstrng);
//Creating Data TAble to hold the data from database temporarily
DataTable dt = new DataTable();
try
{
//SQL Query To Search Categories from DAtabase
String sql = "SELECT * FROM tbl_categories WHERE id LIKE '%"+keywords+"%' OR title LIKE '%"+keywords+"%' OR description LIKE '%"+keywords+"%'";
//Creating SQL Command to Execute the Query
SqlCommand cmd = new SqlCommand(sql, conn);
//Getting DAta From DAtabase
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
//Open DatabaseConnection
conn.Open();
//Passing values from adapter to Data Table dt
adapter.Fill(dt);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
return dt;
}
2. Text Changed Event to Add Search Feature
private void txtSearch_TextChanged(object sender, EventArgs e)
{
//Get the Keywords
string keywords = txtSearch.Text;
//Filte the categories based on keywords
if(keywords!=null)
{
//Use Searh Method To Display Categoreis
DataTable dt = dal.Search(keywords);
dgvCategories.DataSource = dt;
}
else
{
//Use Select Method to Display All Categories
DataTable dt = dal.Select();
dgvCategories.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.
Post a Comment
0 Comments