Jump to content

Search.asp.cs in C#


haibec

Recommended Posts

Hi Guild !I have a table User include field : ID,Username,password . I want make a page Search.aspx.cs for find a user in table User. Please help me code (I'm newbie learning C#). Help me!
Its not too terribly complicated. The only significant part of the search is your SQL query. The way you design your query depends on whether your table has a fulltext index. I'll presume it doesn't for simpliicity.If you're using SQL server, then create a stored procedure like this:
Create procedure Find_Users(@username varchar(50))AsSelect * from users where username = @username/*If you want to match partial names, then use this instead:Select * from users where username like '%' + @username + '%'*/

I assume your want your records displayed in a gridview, with a search textbox and search button at the top. The basic markup for Search.aspx will contain a textbox, a search button, a gridview, and an SQL datasource. The textbox will be bound to one of the select parameters of your SQL datasource. The final product will look something like this:

<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>	<asp:Button ID="btnSearch" runat="server" Text="Search" /><br />	<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"		DataSourceID="sqlSearch">		<Columns>			<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"				SortExpression="ID" />			<asp:BoundField DataField="username" HeaderText="username" SortExpression="username" />		</Columns>	</asp:GridView>	<asp:SqlDataSource ID="sqlSearch" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNetConnectionString %>"		SelectCommand="Find_Users"		SelectCommandType="StoredProcedure">		<SelectParameters>			<asp:ControlParameter ControlID="txtSearch" Name="username" PropertyName="Text" />		</SelectParameters>	</asp:SqlDataSource>

You don't need any code in your code behind, because txtSearch is bound to one of your select parameters. When you click search, the page will automatically display all of the records that match the user you're searching for.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...