Various Types of Search on Database and Display Results on GridView

Introduction

This article explains how to apply various types of search on a database and display the results in a GridView.

Some of you might be trying to create a Search Engine that can be used either for searching the Jobs, Resume, Candidate and so on. In this article I am creating a demo application that will help you to search the database depending on the requirements and then display the results.

Step 1

First of all I created a database in which a few entries of some IT students are made, I named this database "Student", in this database I created a table named IT_Students. It's data is as follows:

search database and show in grid

Step 2

Then I created a new application in Visual Studio.

On this application I took a TextBox, a button and a Grid View:

  1. <div>  
  2.   Search User by Name:-  
  3.    <asp:TextBox runat="server" ID="txt1"></asp:TextBox>  
  4.   <br />  
  5.     <asp:Button ID="btn1" runat="server" Text="Submit" OnClick="btn1_Click" />  
  6. </div>  
  7.   <br />  
  8. <div>  
  9.    <asp:Label ID="lbl1" runat="server" Enabled="false"></asp:Label>  
  10.        <asp:GridView ID="grd" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">  
  11.          <AlternatingRowStyle BackColor="White" />  
  12.          <EditRowStyle BackColor="#2461BF" />  
  13.          <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
  14.          <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
  15.          <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />  
  16.          <RowStyle BackColor="#EFF3FB" />  
  17.          <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />  
  18.          <SortedAscendingCellStyle BackColor="#F5F7FB" />  
  19.          <SortedAscendingHeaderStyle BackColor="#6D95E1" />  
  20.          <SortedDescendingCellStyle BackColor="#E9EBEF" />  
  21.          <SortedDescendingHeaderStyle BackColor="#4870BE" />  
  22.     </asp:GridView>  
  23. </div> 

Step 3

Then I worked on the button click event for the code behind. Provide this code in the .aspx page.

Now I will show various ways to search the database.

First Method

In the first method I used the "%" after the letter that to be searched for, what this will do is it will search the starting letters of all the records and then provide the reasults.

  1. protected void btn1_Click(object sender, EventArgs e)  
  2. {  
  3.     SqlConnection x = new SqlConnection(@"Your Connection");  
  4.     x.Open();  
  5.     SqlDataAdapter da = new SqlDataAdapter("select * from IT_Student where   
  6.     Student_Name like '" + txt1.Text + "%'", x);  
  7.     DataSet ds = new DataSet();  
  8.     da.Fill(ds);  
  9.     grd.DataSource = ds;  
  10.     grd.DataBind();  
  11. } 

Here I first provided the connection to my database using "SqlConnection".

Then I used "SqlDataAdapter" to provide the query for searching the database, this query will search the student names for names that start with the specified letter.

Then I bound the Grid. We can now run our application to see the results.

search database and show in grid

You can see that I provided "a" in the search TextBox and as I clicked on the button all the data with "a" in the beginning of the name is displayed in the Grid.

Second Method

Now I used "%" in the beginning of the letter specified for the search, this searched the student names for that specified letter at the end of them.

So the query was changed to this:

  1. SqlDataAdapter da = new SqlDataAdapter("select * from IT_Student where Student_Name like '%" + txt1.Text + "'", x); 

Then I bound the Grid, and the result is as in the following:

search database and show in grid

You can see that those students are fetched whose name end with "a".

Third Method

Now I used "%" in the beginning and the end for the letter that is to be specified for the search, this searches the student names from both sides, in other words from the beginning and from the end as well.

So the query was changed to this:

  1. SqlDataAdapter da = new SqlDataAdapter("select * from IT_Student where Student_Name like '%" + txt1.Text + "%'", x); 

Then I bound to the Grid and the result is as in the following:

search database and show in grid

You can see that on searching for the "an" all the students whose name has an "an" anywhere is displayed.


Similar Articles