Reader Level:

Searching Records From a DataBase and Displaying In a Gridview Using ASP.Net C#

By Vithal Wadje on Jan 24, 2013
In this article I have explained how to search the records from database and display them in a GridView using a TextBox, ASP.Net and C#.


There is often a need to search the records in a database for those that satisfy a specific condition and display them in a GridView. So in consideration of that requirement I decided to write this article. So let us proceed toward that with step-by-step instructions.

Now before creating the application, let us create a table named emp in a database to store the records in a database table having the fields shown in the following image:

In the preceding table I have created four columns, they 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:

insert into emp values ('vithal','Navi Mumbai','')
insert into emp  values  ('Shivanand','Mumbai','')
insert into emp  values  ('Shivanand','Mumbai','')
insert into emp  values   ('Sudhir','Latur','')

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:

 <form id="form1" runat="server">








        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>



        <asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />





<table><tr><td><p><asp:Label ID="Label2" runat="server" Text="label"></asp:Label>  </p></td></tr></table>


<asp:GridView ID="GridView1" runat="server" >




Now switch to design mode and use the following code.

To bind the grid, create the following method:


 Then double-click on the "Go" button and use the following code:


Now run the application, it will look as in the following:



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:


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



In the all  preceding examples, we learned how to search the records.

  • 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. 
Now we have learned how to search the records from a database and display them in the GridView. I hope this article is useful for all students and beginners. If you have any suggestion related to this article please contact me.

Vithal Wadje
Vithal Wadje

Vithal Wadje currently working on Microsoft Technologies at Mumbai and blogs at as well as C# corner . He has Experience of handling largest financial projects along with Microsoft Technologies Asp.... Read more

Personal Blog: