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:
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>
Now switch to design mode and use the following code.
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();
- }
Then double-click on the "Go" button and use the following code:
- 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 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:
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.