Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I am writing to seek an example, where i can search multiple names in one textbox.

Currently, I am using the following code:

C#
Search Name
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" />
<hr />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    runat="server" AutoGenerateColumns="false" DataSourceID="GridDataSource" AllowPaging="true">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="Item" HeaderText="Item" ItemStyle-Width="150" />
        <asp:BoundField DataField="desc" HeaderText="Description" ItemStyle-Width="150" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="GridDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConStr %>"
    SelectCommand="SELECT Name, item, desc FROM deal" FilterExpression="Name LIKE '{0}%'">
    <FilterParameters>
        <asp:ControlParameter Name="Name" ControlID="txtSearch" PropertyName="Text" />
    </FilterParameters>
</asp:SqlDataSource>
Posted
Updated 16-Jan-14 0:52am
v4
Comments
Richard MacCutchan 20-Dec-13 7:00am    
I am writing to seek help or example
Then you need to provide a lot more detail. Do you want to search for items in the textbox, or do you want to search somewhere else?
miss786 20-Dec-13 7:43am    
Thank you so much for your help and response. Apology for not making the problem clear enough. I was looking for an example, where I can search items in one textbox. I really appreciate your time. Many thanks

1 solution

Assuming you mean you want a textbox which contains two or more names and you want to search some database for one or more of the names...
It's not quite that simple. First, you need your user to indicate where a name ends: so get him to type commas or semicolons for example:
C#
dave, pete, mike
Then assuming you are talking about SQL, you need to convert this to a format that SQL can accept:
C#
string s = "dave, pete, mike";
string[] names = s.Split(',').Select(n => string.Format("'{0}'", n.Trim())).ToArray();
string sql = string.Format("SELECT * FROM MyTables WHERE username IN ({0})", string.Join(",", names));

But...That is pretty dangerous - it leaves you wide open to SQL Injection attack, so you will probably have to sanitize your user input.
 
Share this answer
 
Comments
Karthik_Mahalingam 20-Dec-13 7:41am    
excellent...
miss786 20-Dec-13 7:42am    
Thank you for suggestion and example. I really appreciate your help. I shall have a go, implementing my solution using your code as framework. Many thanks for your time and help.
OriginalGriff 20-Dec-13 8:10am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900