Jump to content

Search.asp.cs in C#


haibec
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...