Skip to main content

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','abc@gmail.com') 
insert into emp  values  ('Shivanand','Mumbai','xyz@yahoo.com')
insert into emp  values  ('Shivanand','Mumbai','xyz@yahoo.com')
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:


 <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

Comments

Popular posts from this blog

.NET Core API With Dapper, Repository And UnitOfWork

  Introduction In this tutorial, you will use ASP.NET Core Web API to create a web API that returns a list of brands. This article focuses on creating web API using dapper, repository pattern, and UnitOfWork pattern in .NET 8. I will guide you through the steps by step to create web API. HTTP is not just for serving up web pages. HTTP is also a powerful platform for building APIs that expose services and data. HTTP is simple, flexible, and ubiquitous. Almost any platform that you can think of has an HTTP library, so HTTP services can reach a broad range of clients, including browsers, mobile devices, and traditional desktop applications. ASP.NET Core Web API is a framework for building web APIs on top of the .NET Framework. Creating a New Project in Visual Studio Launch Visual Studio and select Create a New Project. Creating a Core Web API Start Visual Studio and choose Create a new project. In the Create a new project dialog, select ASP.NET Core Web API Click Next. Configuring You...

.Net framework interview questions

What is the .NET Framework? The .NET Framework is a set of technologies that form an integral part of the .NET Platform. It is Microsoft’s managed code programming model for building applications. The .NET Framework has two main components: Common Language Runtime (CLR): The CLR is one of the foundation in the .NET framework and provides a common set of services for applications developed on Microsoft .Net Technologies. .NET Framework class library: The .NET framework class library is a collection of reusable types and exposes features of the runtime. It contains of a set of classes that is used to access common functionality. What is CTS (Common Type System)? The common type system (CTS) defines how types are declared, used, and managed in the runtime, and is also an important part of the runtime’s support for cross-language integration. The common type system performs the following functions: ·          Establishes a framework that...

Move Selected Gridview Rows to Another Gridview in Asp.net

Introduction :  Here I will explain how to move or transfer selected checkbox gridview rows to another  grid v iew in asp.net using c#, vb.net or copy one gridview row to another gridview in asp.net using c#, vb.net. Description :  In previous posts I explained Display images in gridview from database in asp.net, Enable/Disable checkbox in gridview based on condition,  asp.net gridview examples  and  bind data to textbox control in gridview ,  Bind data to dropdownlist  in gridview  and many articles relating to  asp.net,  c#, vb.net, c#,vb.net. Now I will explain how to move selected rows from one gridview to another gridview in asp.netusing c#, vb.net. To implement this first we need to write the code in aspx page like this  < html   xmlns ="http://www.w3.org/1999/xhtml"> < head > ...