Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

Searching from a form

A search box turns a list into a tool. The visitor types a term and the grid narrows to matches.

Add a parameter to the data source

Markup
<asp:SqlDataSource id="productsSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:Shop %>"
    SelectCommand="SELECT Id, Name FROM Product WHERE Name LIKE @term">
    <SelectParameters>
        <asp:ControlParameter Name="term" ControlID="txtSearch"
            PropertyName="Text" DefaultValue="%" />
    </SelectParameters>
</asp:SqlDataSource>

Points worth noting

  • The parameter is filled from a control, so no string building is needed.
  • A default value keeps the grid sensible before the visitor types anything.
  • The query still runs as a parameterised command, which is the safe approach.
Wildcards. A pattern search needs wildcard characters around the term. Add them in the parameter value, not inside the query text.

When the visitor needs to add data rather than find it, move to inserting a row from a form.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.