Alomiry
asked on
Bulid a serach feature .
Hello,
I'm trying to build a search feature where a user enters a text value and the page returns records where the string appears in a specific field.
Can help me to find tutorials to explain the steps of building search feature by using LIKE/ SQL.
Regards
I'm trying to build a search feature where a user enters a text value and the page returns records where the string appears in a specific field.
Can help me to find tutorials to explain the steps of building search feature by using LIKE/ SQL.
Regards
You can do that using a gridview.
Here is an example of search. This actually works because I use it:
You can also see more tutorials here.
http://www.codeproject.com/KB/custom-controls/GridViewSearch.aspx
Here is an example of search. This actually works because I use it:
<form id="form1" runat="server">
<div style="text-align:center">
<asp:Label ID="enterNamebutton" Text="<b>Enter Name</b>" runat="server" />
<asp:TextBox ID="txtSearch" runat="server" style="height:23px"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" TabIndex="1" height="27px" Text="GO" OnClick="btnSearch_Click" class="btn" />
<asp:Button ID="btnClear" OnClick="ClearFilter" height="27px" Text="Reset" runat="server" class="btn" />
</div>
<br /><br />
<div style="text-align:left">
<div style="text-align:center">SEARCH By Last Name, First Name (e.g.; Doe, John) and click the <div style="color:#69e">Go</div> button
Click the <div style="color:#69e;white-space:nowrap">Reset</div> button to SEARCH again<br /><br />
<asp:GridView ID="GridView1" BorderWidth="0" runat="server" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" PageSize="15" DataSourceID="APOMembers" EmptyDataText="No data found" Width="564px">
<HeaderStyle BackColor="#677617" ForeColor="White" Font-Size="Small" Font-Names="Verdana" width="100%" />
<RowStyle BackColor="White" Font-Size="Small" Font-Names="Verdana" BorderStyle="None" />
<AlternatingRowStyle BackColor="Gainsboro" width="100%" />
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="lnkname" runat="server" Text='<%#Eval("Name") %>' PostBackUrl='<%#"~/Details.aspx?ID="&Eval("ID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Cell_Phone" HeaderText="Cell Phone" SortExpression="Cell_Phone" >
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="Phone" HeaderText="Home Phone" SortExpression="Phone" >
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="Email_Add" HeaderText="Email Address" SortExpression="Email_Add" HtmlEncode="False">
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="Members" runat="server" ConnectionString="<%$ ConnectionStrings:APOConnectionString %>"
ProviderName="<%$ ConnectionStrings:APOConnectionString.ProviderName %>"
SelectCommand="SELECT ID, ([firstName]&' '& [LastName]) as Name, [Cell_Phone], [Phone], [Email_Add] FROM Africa WHERE (firstName LIKE '' & @firstName & '%' OR lastname LIKE '' & @lastName & '%')">
<SelectParameters>
<asp:ControlParameter ControlID="txtSearch" Name="firstName" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="txtSearch" Name="lastName" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</div>
</form>
You can also see more tutorials here.
http://www.codeproject.com/KB/custom-controls/GridViewSearch.aspx
ASKER
Thanks, It's good tutorials.
But i'm working on Hotel Booking site and i have to use the search to look for available hotels.
Can you help to find the best way to do my task ?
Regards
But i'm working on Hotel Booking site and i have to use the search to look for available hotels.
Can you help to find the best way to do my task ?
Regards
How are you looking for available hotels?
Do you have a database of a list of hotels?
Do you have a database of a list of hotels?
ASKER
I have a database for the hotels and rooms available.
and I\m using that way>
string SearchType = Request.Params["id_input"] ;
query.CommandText = " SELECT Name,Type FROM Hotels WHERE (Name LIKE '" + SearchType + "' + '%' )";
Regards
and I\m using that way>
string SearchType = Request.Params["id_input"]
query.CommandText = " SELECT Name,Type FROM Hotels WHERE (Name LIKE '" + SearchType + "' + '%' )";
Regards
Try these and let me know. If you have a code different from this, show where you are having issues.
Please change whatever needs changing.
I am not c# guy but I think the codeFile looks good.
Please change whatever needs changing.
I am not c# guy but I think the codeFile looks good.
//Hotels.aspx
<%@ Page Language="C#" Debug="true" AutoEventWireup="false" Inherits="hotels" CodeFile="hotels.aspx.vb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Available Hotels</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center">
<asp:Label ID="enterNamebutton" Text="<b>Enter Name</b>" runat="server" />
<asp:TextBox ID="SearchType" runat="server" style="height:23px"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" TabIndex="1" height="27px" Text="GO" OnClick="btnSearch_Click" class="btn" />
<asp:Button ID="btnClear" OnClick="ClearFilter" height="27px" Text="Reset" runat="server" class="btn" />
</div>
<br /><br />
<div style="text-align:left">
<div style="text-align:center">SEARCH By Name, and click the <div style="color:#69e">Go</div> button
Click the <div style="color:#69e;white-space:nowrap">Reset</div> button to SEARCH again<br /><br />
<asp:GridView ID="GridView1" BorderWidth="0" runat="server" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" PageSize="15" DataSourceID="Hotels" EmptyDataText="No data found" Width="564px">
<HeaderStyle BackColor="#677617" ForeColor="White" Font-Size="Small" Font-Names="Verdana" width="100%" />
<RowStyle BackColor="White" Font-Size="Small" Font-Names="Verdana" BorderStyle="None" />
<AlternatingRowStyle BackColor="Gainsboro" width="100%" />
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="name" >
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" >
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="Hotels" runat="server" ConnectionString="<%$ ConnectionStrings:HotesConnectionString %>"
ProviderName="<%$ ConnectionStrings:HotesConnectionString.ProviderName %>"
SelectCommand="SELECT Name,Type FROM Hotels WHERE (Name LIKE '' & @SearchType & '%')">
<SelectParameters>
<asp:ControlParameter ControlID="SearchType" Name="SearchType" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</div>
</form>
</body>
</html>
//Hotels.aspx.cs
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;
public partial class Hotels : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSearch_Click(object sender, EventArgs e)
{
GridView1.DataBind();
}
protected void ClearFilter(object sender, EventArgs e)
{
// remove the filter and rebind the grid
this.txtSearch.Text = string.Empty;
this.GridView1.DataBind();
}
}
//web.config: Add to your web.config or create yours
<connectionStrings>
<add name="HotesConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=/inetpub/wwwroot/Hotels/App_Data/yourDB.mdb" providerName="System.Data.OleDb" />
</connectionStrings>
Sorry change this line:
this.txtSearch.Text = string.Empty;
To
this.SearchType.Text = string.Empty;
this.txtSearch.Text = string.Empty;
To
this.SearchType.Text = string.Empty;
ASKER
But still working with grid view and that solution i don't like it, because it's a easy way .
Thanks any way
Regards
Thanks any way
Regards
What exactly are you looking for?
Can you be more specific?
Can you be more specific?
ASKER
I'm looking to create search for my hotel booing system ?
And the result get from the database, and you can search by CITY,COUNTRY, NUMBER OF ROOM, NUMBER OF NIGHT. like www.hotel.com
Regards
And the result get from the database, and you can search by CITY,COUNTRY, NUMBER OF ROOM, NUMBER OF NIGHT. like www.hotel.com
Regards
Well if you want to use all those as your search param, then use this code:
<asp:TextBox id="txtSearch" runat="server" />
strSQLQuery = "SELECT Name,Type FROM Hotels " _
& "FROM Hotels" _
& "WHERE city LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "OR country LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "OR [NUMBER OF ROOM] LIKE '%" & Replace(strSearch, "'", "''") & "%' " _ & "OR [NUMBER OF NIGHT] LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "ORDER BY city;"
More like this:
Same code; just formatting issues
<asp:TextBox id="txtSearch" runat="server" />
strSQLQuery = "SELECT Name,Type FROM Hotels " _
& "FROM Hotels" _
& "WHERE city LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "OR country LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "OR [NUMBER OF ROOM] LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "OR [NUMBER OF NIGHT] LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "ORDER BY city;"
Same code; just formatting issues
And since you are using c#, I *think* it is something like this:
<asp:TextBox id="SearchType" runat="server" />
query.CommandText = "SELECT Name,Type FROM Hotels " _
& "FROM Hotels" _
& "WHERE city LIKE ' " + Replace(SearchType, "'", "''") + "%' " _
& "OR country LIKE ' " + Replace(SearchType, "'", "''") + "%' " _
& "OR [NUMBER OF ROOM] LIKE ' " + Replace(SearchType, "'", "''") + "%' " _
& "OR [NUMBER OF NIGHT] LIKE '" + Replace(SearchType, "'", "''") + "%' " _
& "ORDER BY city;"
also, these Replace(SearchType, "'", "''") are to protect from sql injection.
Read about sql inject here
http://msdn.microsoft.com/en-us/library/ms161953.aspx
Read about sql inject here
http://msdn.microsoft.com/en-us/library/ms161953.aspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good work
ASKER