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:

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:
- 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:
- "Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" -> "New" -> "Project..." -> "C#" -> "Empty Project" (to avoid adding a master page).
- Give the project a name, such as "SearchRecords" or another as you wish and specify the location.
- Then right-click on Solution Explorer and select "Add New Item" then create a "Default.aspx" page.
- 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>
To bind the grid, create the following method:
- private void rep_bind()
- {
- connection();
- string query = "select * from emp where Name like '" + TextBox1.Text + "%'";
- SqlDataAdapter da = new SqlDataAdapter(query, con);
- DataSet ds = new DataSet();
- da.Fill(ds);
- GridView1.DataSource = ds;
- GridView1.DataBind();
- }
- if (dr.HasRows)
- {
- dr.Read();
- rep_bind();
- GridView1.Visible = true;
- TextBox1.Text = "";
- Label1.Text = "";
- }
- else
- {
- GridView1.Visible = false;
- Label1.Visible = true;
- Label1.Text = "The search Term " + TextBox1.Text + " Is Not Available in the Records";
- }

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:

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


Talon YuPosted Jan 29, 2021, 12:47 AM EST
Is it possible to change the code to do search for multiple records? The current code allows to search for 1 record and display that 1 record. If I want to search for another record, it will still be displayed, but replaces that first record instead of displaying it under the first record. Is there anyway to change it? Thanks.
Rea JPosted May 14, 2020, 8:33 AM EDT
The search isn't working for me. the rest is all fine. help please
Ravishankar VelladuraiPosted Mar 23, 2020, 1:27 AM EDT
This article most helpful to me. Thanks
Bas MaPosted Aug 28, 2019, 9:49 AM EDT
Thanks for explanation
indradeo kumarPosted Aug 3, 2019, 1:07 AM EDT
Dropdown list value come from first table and gridview is sencond table
indradeo kumarPosted Aug 3, 2019, 1:06 AM EDT
How to make search function using dropdownlist and display in gridview.
Vriti SharmaPosted Aug 15, 2018, 7:40 AM EDT
How to search checked checkbox items in gridview
Vriti SharmaPosted Aug 15, 2018, 7:40 AM EDT
Great article
Đăng HảiPosted Jun 7, 2018, 10:49 PM EDT
I'm sure you missed some codes on the Go button. I can't see any definition of "dr"
Babu AliPosted Apr 16, 2018, 11:13 AM EDT
I didn't get about dr from where it come..?? It gives error on my code..
Ajay SinghPosted Feb 28, 2018, 2:36 AM EST
What if search not found?
Netra GhagPosted Jan 21, 2018, 5:56 AM EST
Simply Gr8 thnaaaaaaqqqqqq soooo muchhhh for sharingggg this code :) thak you once again
Shankar BellurkarPosted Nov 20, 2017, 11:34 AM EST
What is dr.HasRows?
Zola ManuelPosted May 24, 2017, 1:19 AM EDT
Hi, could you please help as i keep getting an error when i write dr on this codes..
Pratik ThakrePosted May 18, 2017, 9:22 PM EDT
How to search article with any word like your page search box show
prince bhattPosted Mar 5, 2017, 8:16 PM EST
Hey just put the rep_bind() method code inside the go button .you never face any error
Nomfundo CindiPosted Feb 20, 2017, 1:45 AM EST
Thank you the article, please explain this rep_bind(); it give error on my code.
shiva chinthalaPosted Dec 15, 2016, 4:46 PM EST
Hi,nice article but i am having one doubt whether this text box can accept the input as both alphanumerical or only alpha sets.
Farhan KhanPosted Sep 24, 2016, 1:45 AM EDT
How to integrate autocomplete in this
Vithal WadjeTOP 100Posted Sep 11, 2016, 8:28 AM EDT
Thanks
SubashPosted Sep 9, 2016, 11:27 PM EDT
Nice
Fernando ChachaPosted Aug 31, 2016, 6:44 AM EDT
My gridview has page 1, 2, 3..., so when I go to page 2 for example , it doesn't view the data according the specified criterion in the search textbox.
Vithal WadjeTOP 100Posted Aug 31, 2016, 6:22 AM EDT
Can you explain in detail
Fernando ChachaPosted Aug 31, 2016, 5:59 AM EDT
Thanks Vithal, i have a doubt my gridview is paged when i click in page 2, it shows data not specified in parameter
Vithal WadjeTOP 100Posted Aug 20, 2016, 6:35 PM EDT
Create data table dynamically as per column list coming from database
Wessel OosthuizenPosted Aug 19, 2016, 12:15 AM EDT
Hi Vithal, I have 4 gridviews and i want each gridview to show a different column. How will i accomplish that. Thanks for your help
Vithal WadjeTOP 100Posted Jul 22, 2016, 6:58 AM EDT
Welcome
Hanif HefazPosted Jul 21, 2016, 11:25 PM EDT
Thank you sir for the nice aricle
Vithal WadjeTOP 100Posted Jul 15, 2016, 4:20 AM EDT
Thanks
Suresh MolabantiPosted Jul 14, 2016, 5:26 AM EDT
Good one...
Vithal WadjeTOP 100Posted Jun 20, 2016, 10:42 PM EDT
thanks
RajaPosted Jun 15, 2016, 9:55 PM EDT
Good Work....
Vithal WadjeTOP 100Posted May 30, 2016, 6:38 AM EDT
Its simple add two more conditions
veera muthuPosted May 27, 2016, 9:10 PM EDT
nice work...but if i am using one more tools like two textbox and one dropdownlist? how to search gridview records.. help me??
Vithal WadjeTOP 100Posted Mar 13, 2016, 12:11 PM EDT
yes Nyra Leo you can get it. just append query output with + operator
Vithal WadjeTOP 100Posted Mar 13, 2016, 12:10 PM EDT
sai refer my other articles
Nyra LeoPosted Mar 6, 2016, 6:00 AM EST
can I get this searched column elements in a variable Like if i have search query like" select telephoneno from table1 where city=Mumbai " then i want to take all telephone nos (seperated with commas)in a variable .Any idea hw can i do this?
AshokPosted Feb 19, 2016, 12:28 AM EST
ho to achieve both like and in condition here in the above article u implemented for like lets say i have 2 names arokya,ajit if i want to get based on these 2 names how to acevive this.Thanks
Vithal WadjeTOP 100Posted Dec 16, 2015, 7:43 AM EST
Thanks
Hari ShankerPosted Dec 16, 2015, 7:09 AM EST
nice article
Vithal WadjeTOP 100Posted Oct 12, 2015, 6:21 AM EDT
dr has no rows ,check whether the records are coming properly or not .
Pavan BujjiPosted Oct 11, 2015, 11:46 PM EDT
i have an error as Object reference not set to an instance of an object at the if(dr.has rows) for same above problem..how can i solve this problem..pleas tell me sir.
Vithal WadjeTOP 100Posted Sep 7, 2015, 9:33 AM EDT
refer my latest article
Niren GuptaPosted Jul 22, 2015, 2:26 AM EDT
INSTEAD OF GRID VIEW CAN I USE TABLE CONTROL IF YES THEN HOW ? MY CODE IS UNABLE TO DO THE SHORTING AS I USED TABLE CONTROL INSTEAD OF GRID VIEW?
Abhijit PatilPosted Jan 15, 2015, 4:52 PM EST
Hey usman khan do the same operation but change only query select * from bikemaster where name = ``+ textbox1.text +`` and city =`` +dropdownlist1.selected value +``; hope it helps
Abhijit PatilPosted Jan 15, 2015, 4:43 PM EST
Hey Nihalani Pankaj if you are not familiar with disconnected. ArcArchiecture ,refer my artical http://www.c-sharpcorner.com/UploadFile/8af593/ado-net-dis-connected-architecture/ hope it helps..
Vithal WadjeTOP 100Posted Jan 3, 2015, 3:12 AM EST
Welcome
usman khanPosted Jan 3, 2015, 2:39 AM EST
ok sure thanks
Vithal WadjeTOP 100Posted Jan 3, 2015, 1:51 AM EST
usman khan sir give me some time to write article on this or refer C#corner asp.net article list defiantly you will find solution
usman khanPosted Jan 2, 2015, 11:49 PM EST
Hey Guys! I need some help.. I am building an auto buy and sell website for my semester project in which I want to include a filter search i.e I want that the users select from two dropdownlists and then search...like the two dropdownlist will be City(in which city do you want to buy) and make(like Toyota, Honda etc)...so by selecting these and clicking on search button it should fetch the records from database and show these records in data list..thats all
Vithal WadjeTOP 100Posted Oct 30, 2014, 10:46 PM EDT
make necessary changes as per your server location
Nihalani PankajPosted Oct 30, 2014, 10:29 PM EDT
Error come that no constructor defined how to solve this error i download the zip file but solve this error
Vithal WadjeTOP 100Posted Oct 29, 2014, 8:02 PM EDT
its a dataReader ,download zip file for better understanding
Nihalani PankajPosted Oct 29, 2014, 8:00 PM EDT
Which Method is dr.read and please give me code of this method
Nihalani PankajPosted Oct 29, 2014, 7:54 PM EDT
What is dr is that an dataset or what ?
Vithal WadjeTOP 100Posted Oct 27, 2014, 11:24 PM EDT
balaji badrinath its fetch the records like character given input
Vithal WadjeTOP 100Posted Oct 27, 2014, 11:22 PM EDT
Nihalani Pankaj dr.hasrows checks whether it has rows and dr.Read means it reads rows from dataReader
Nihalani PankajPosted Oct 27, 2014, 8:40 PM EDT
What is that dr.hasrows and dr.read
praveen kumarPosted Jul 3, 2014, 1:55 AM EDT
what is that dr.hasrows and dr.read
balaji badrinathPosted Jun 7, 2014, 4:15 AM EDT
string query = 'select * from emp where Name like...in this one what is like?
Vithal WadjeTOP 100Posted May 26, 2014, 11:46 AM EDT
thanks sir
Imran KaziPosted May 24, 2014, 11:55 PM EDT
good work
shruti beriwalPosted May 14, 2014, 10:14 AM EDT
showing a nullreference exception was handled by user code in the following line:::::::::::::::::;constr = ConfigurationManager.ConnectionStrings["emp"].ToString();
Vithal WadjeTOP 100Posted May 4, 2014, 9:13 AM EDT
thanks,refer my article list
pushpa cpPosted Apr 30, 2014, 8:00 AM EDT
gud article it worked bt may i know how can i export it into excel or word pls help with this as soon as possible
pushpa cpPosted Apr 30, 2014, 7:59 AM EDT
gud article it worked.....
Vithal WadjeTOP 100Posted Apr 23, 2014, 11:45 AM EDT
thanks saddique khan sir
saddique khanPosted Apr 21, 2014, 10:06 PM EDT
It is good article
Vithal WadjeTOP 100Posted Apr 21, 2014, 9:00 AM EDT
its my pleasure Abdullah Naeb sir
Abdullah NaebPosted Apr 17, 2014, 11:53 AM EDT
Thanks a lot
Vivek DubeyPosted Apr 7, 2014, 9:47 PM EDT
how we'll define the datareader dr?
pranay kumarPosted Nov 17, 2013, 7:54 PM EST
Thanks
Vithal WadjeTOP 100Posted Sep 30, 2013, 11:48 AM EDT
thanks Nguyen Minh Triet sir
Nguyen Minh TrietPosted Sep 29, 2013, 1:32 AM EDT
Thanks, it's works for me
Vithal WadjeTOP 100Posted Sep 23, 2013, 6:33 AM EDT
thanks ketan
ketan italiyaPosted Sep 19, 2013, 9:51 PM EDT
nice work thanks.
Vithal WadjeTOP 100Posted Aug 19, 2013, 3:40 AM EDT
thanks and welcome Mohamed Ali sir
Mohamed AliPosted Aug 19, 2013, 3:39 AM EDT
Thanks to C# conner Community