Search Database, Display In ASP.NET GridView Using C#

In this article, I have explained how to search a database table and display result in an ASP.NET GridView control using C# and ADO.NET.

Background

There is often a need to search records in a database for those that satisfy a specific condition and display them in a GridView in an ASP.NET application. So in consideration of that requirement, I've written this article. So let us proceed and follow these step-by-step instructions.

Before we create our Web application, let us create a database table named emp in SQL Server. The table looks like the following:
 
emptbale.png

In the above table, I have created four columns. These columns are are id for the unique identity, Name for the emp name, address for emp address and email to store the email address of the emp.

Now insert some records into the table as you wish, such as:
  1. insert into emp values ('vithal','Navi Mumbai','abc@gmail.com')   
  2. insert into emp  values  ('Shivanand','Mumbai','xyz@yahoo.com')  
  3. insert into emp  values  ('Shivanand','Mumbai','xyz@yahoo.com')  
  4. insert into emp  values   ('Sudhir','Latur','ac12@gmail.com.com')  

I hope you have created the same type of table.

Now let us start to create an application to search the records, step-by-step.

Now create the project  as:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" -> "New" -> "Project..." -> "C#" -> "Empty Project" (to avoid adding a master page).
  3. Give the project a name, such as "SearchRecords" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer and select  "Add New Item" then create a "Default.aspx" page. 
  5. Add a button, a label and a GridView in the Default.aspx page.
Then the <form> section of the Default aspx page will look as in the following:
  1. <form id="form1" runat="server">  
  2.     <div>    
  3.    <table>  
  4.     <tr>  
  5.     <td>   
  6.        Search  
  7.         </td>  
  8.         <td>  
  9.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  10.         </td>  
  11.         <td>   
  12.         <asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />  
  13.         </td>  
  14.         </tr>  
  15. </table>  
  16. <table><tr><td><p><asp:Label ID="Label2" runat="server" Text="label"></asp:Label>  </p></td></tr></table>  
  17. <asp:GridView ID="GridView1" runat="server" >  
  18.     </asp:GridView>   
  19.     </div>  
  20. </form>  
Now switch to design mode and use the following code.

To bind the grid, create the following method:

  1. private void rep_bind()  
  2. {  
  3.     connection();  
  4.     string query = "select * from emp where Name like '" + TextBox1.Text + "%'";  
  5.     SqlDataAdapter da = new SqlDataAdapter(query, con);  
  6.     DataSet ds = new DataSet();  
  7.     da.Fill(ds);  
  8.     GridView1.DataSource = ds;  
  9.     GridView1.DataBind();  
  10. }  
Then double-click on the "Go" button and use the following code:

  1. if (dr.HasRows)  
  2. {  
  3.     dr.Read();  
  4.     rep_bind();  
  5.     GridView1.Visible = true;  
  6.     TextBox1.Text = "";  
  7.     Label1.Text = "";  
  8. }  
  9. else  
  10. {  
  11.     GridView1.Visible = false;  
  12.     Label1.Visible = true;  
  13.     Label1.Text = "The search Term " + TextBox1.Text + " Is Not Available in the Records";  
  14. }  
Now run the application, it will look as in the following:

 

demo.png

 

Now enter some characters in the TextBox that do not match the specified table's records (that we inserted into the table); that will produce the following message: 

 

Not Found.png

Now do not enter a value into the TextBox and click on the "Go" button, that will display all the records as in the following:

 

 

default.png

Now enter the specific name and click on the "Go" button to search the records specific to name as follows:

 

 

matchfound.png

 

In all  preceding examples, we learned how to search a database table records. 

Note 
  • For detailed code please download the zip file attached above.
  • Don't forget to apply the relevant changes in the Web.config file depending on your Server location. 
Summary
 
We've learned how to search the records in a database and display them in a GridView control. I hope this article is useful for all students and beginners.