This tutorial shows how simple it is to allow users to search a database for matching text they input themselves. C# version.
Searching a website is often taken for granted. Implementing a search facility on a website used to be rather complex. However, as with many things in ASP.NET, it has gotten much easier. This tutorial will show how we can implement a simple search facility to allow users to input text and search a database for matching records.
First, we need to add the following assembly reference:
| using System.Data.SqlClient; |
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
In the Web.config, we declare the connection string:
<appSettings>
<add key="ConnString" value="Data Source=CLIENT-TASK2\SQLEXPRESS;Initial Catalog=BasicDataAccess;Integrated Security=True"/> </appSettings> |
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
The ASPX page will consist of a textbox, a button and a repeater control to display the results. It will look something like this:
<form id="form1" runat="server"> Search: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" /><br /><br /> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate><table width="100%"><tr><th>Name</th><th>City</th></tr></HeaderTemplate> <ItemTemplate>
<tr><td><%#DataBinder.Eval(Container.DataItem, "theName")%></a></td> <td><%#DataBinder.Eval(Container.DataItem, "theCity")%></td></tr> </ItemTemplate> <FooterTemplate></table></FooterTemplate> </asp:Repeater> </form> |
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
In the code-behind, we will reference a Stored Procedure, which will be something like this:
| ALTER PROCEDURE spSearchByString
@SearchString varchar(50) AS
SELECT [tblOne].theName, [tblOne].theCity FROM [tblOne] WHERE ([tblOne].theName LIKE '%' + @SearchString + '%' OR [tblOne].theCity LIKE '%' + @SearchString + '%') RETURN |
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
The code-behind should look something like this:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (Request.QueryString["searchString"] != null) {
DisplaySearchResults(Request.QueryString["searchString"]); } } public void DisplaySearchResults(string strSearch) {
SqlCommand cmd = new SqlCommand("spSearchByString", new SqlConnection(ConfigurationManager.AppSettings["ConnString"])); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@SearchString", strSearch); cmd.Connection.Open();
Repeater1.DataSource = cmd.ExecuteReader(); Repeater1.DataBind();
cmd.Connection.Close(); cmd.Connection.Dispose(); } protected void Button1_Click(object sender, EventArgs e) {
Response.Redirect("Default.aspx?searchString=" + Server.UrlEncode(TextBox1.Text)); } } |
Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!