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

Background

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:
 
emptbale.png

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','[email protected]')
insert into emp  values  ('Shivanand','Mumbai','[email protected]')
insert into emp  values  ('Shivanand','Mumbai','[email protected]')
insert into emp  values   ('Sudhir','Latur','[email protected]')

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">

    <div>  

   <table>

    <tr>

    <td> 

       Search

        </td>

        <td>

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

        </td>

        <td> 

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

        </td>

        

        </tr>
 

</table>

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

 

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

    </asp:GridView> 

    </div>

 </form>


Now switch to design mode and use the following code.

To bind the grid, create the following method:


Bindgrid.png

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

buttonclick.png
 

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 the all  preceding examples, we learned how to search the 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
 
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.

Up Next
    Ebook Download
    View all
    Learn
    View all