Puresilence
Puresilence

Reputation: 129

How to search a database in ASP.NET using a text box

I got a quick question, I am building an ASP.NET website and have a database set up (SQL Server 2008). One one of my pages I am displaying all of all of the entries of the database with a search text box at the top. How would I go about taking the value that the user entered and using that string to display only the data entries that apply to it.

Here is my code for the .aspx file

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Layout.Master" CodeBehind="db.aspx.vb" Inherits="Project4.db" %>

    <fieldset>
        <legend>Search</legend>
        Last Name: <asp:TextBox ID="LastNameSearch" runat="server"></asp:TextBox>
        <asp:Button ID="Submit" runat="server" Text="Search" /> <br />
    </fieldset>



<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
    AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" 
    BorderStyle="None" BorderWidth="1px" CellPadding="4" 
    DataKeyNames="FirstName,LastName,PhoneNum" AllowPaging="True">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
            SortExpression="FirstName" ReadOnly="True" />
        <asp:BoundField DataField="LastName" HeaderText="LastName"
            SortExpression="LastName" ReadOnly="True" />
        <asp:BoundField DataField="PhoneNum" HeaderText="PhoneNum"
            SortExpression="PhoneNum" ReadOnly="True" />
        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
        <asp:CommandField ShowDeleteButton="True" />
    </Columns>
    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
    <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
    <RowStyle BackColor="White" ForeColor="#003399" />
    <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
    <SortedAscendingCellStyle BackColor="#EDF6F6" />
    <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
    <SortedDescendingCellStyle BackColor="#D6DFDF" />
    <SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:Database1ConnectionString %>" 
    SelectCommand="SELECT [FirstName], [LastName], [PhoneNum], [Email] FROM [MailList]" 
    ViewStateMode="Disabled" OldValuesParameterFormatString="original_{0}">

</asp:SqlDataSource>

Now I want to search the string and compare it to the second column of the database the LastName and only display the records where the last name matches that. Any help would be greatly appreciated

Upvotes: 0

Views: 11682

Answers (2)

Nudier Mena
Nudier Mena

Reputation: 3274

you have to configure your datatabase to performs contains and freetext querys, that way you can perform a search onto your database base on the data that match your fields criteria.

how to search a database

Upvotes: 0

JCherryhomes
JCherryhomes

Reputation: 426

This should be pretty close, doing it from memory though so it may be off a little. It should be enough to get you going in the right direction though I think

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:Database1ConnectionString %>" 
    SelectCommand="SELECT [FirstName], [LastName], [PhoneNum], [Email] FROM [MailList] WHERE [LastName] = @lastName" 
    ViewStateMode="Disabled" OldValuesParameterFormatString="original_{0}">
    <SelectParameters>
        <asp:ControlParameter ControlID="LastNameSearch" Name="lastName" defaultValue="" />
    </SelectParameters>
</asp:SqlDataSource>

Upvotes: 1

Related Questions