title: How to search a database in ASP.NET using a text box
body: <p>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. </p>
  
  <p>Here is my code for the .aspx file</p>
  
  <pre><code>&lt;%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Layout.Master" CodeBehind="db.aspx.vb" Inherits="Project4.db" %&gt;
  </code></pre>
  
  <p>
  
  </p>
  
  <pre><code>    &lt;fieldset&gt;
          &lt;legend&gt;Search&lt;/legend&gt;
          Last Name: &lt;asp:TextBox ID="LastNameSearch" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;asp:Button ID="Submit" runat="server" Text="Search" /&gt; &lt;br /&gt;
      &lt;/fieldset&gt;
  
  
  
  &lt;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"&gt;
      &lt;Columns&gt;
          &lt;asp:CommandField ShowEditButton="True" /&gt;
          &lt;asp:BoundField DataField="FirstName" HeaderText="FirstName" 
              SortExpression="FirstName" ReadOnly="True" /&gt;
          &lt;asp:BoundField DataField="LastName" HeaderText="LastName"
              SortExpression="LastName" ReadOnly="True" /&gt;
          &lt;asp:BoundField DataField="PhoneNum" HeaderText="PhoneNum"
              SortExpression="PhoneNum" ReadOnly="True" /&gt;
          &lt;asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /&gt;
          &lt;asp:CommandField ShowDeleteButton="True" /&gt;
      &lt;/Columns&gt;
      &lt;FooterStyle BackColor="#99CCCC" ForeColor="#003399" /&gt;
      &lt;HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" /&gt;
      &lt;PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" /&gt;
      &lt;RowStyle BackColor="White" ForeColor="#003399" /&gt;
      &lt;SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" /&gt;
      &lt;SortedAscendingCellStyle BackColor="#EDF6F6" /&gt;
      &lt;SortedAscendingHeaderStyle BackColor="#0D4AC4" /&gt;
      &lt;SortedDescendingCellStyle BackColor="#D6DFDF" /&gt;
      &lt;SortedDescendingHeaderStyle BackColor="#002876" /&gt;
  &lt;/asp:GridView&gt;
  &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="&lt;%$ ConnectionStrings:Database1ConnectionString %&gt;" 
      SelectCommand="SELECT [FirstName], [LastName], [PhoneNum], [Email] FROM [MailList]" 
      ViewStateMode="Disabled" OldValuesParameterFormatString="original_{0}"&gt;
  
  &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p></p>
  
  <p>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 </p>