SQL LIKE Clause With CommandParamters in ASP.NET

Posted on 2006-05-25
Last Modified: 2007-12-19
SelectCommand="SELECT id, firstname, lastname, code, phone FROM dbo.Customers WHERE (firstname LIKE @firstname '%') OR (lastname LIKE @lastname)"

I need to be able to look up Firstnames and lastnames using the Like% clause ( text/wildcard ). How can I implement this wit the command paramters in ASP.NET?
Question by:developernetwork
    LVL 28

    Expert Comment

    SelectCommand="SELECT id, firstname, lastname, code, phone FROM dbo.Customers WHERE (firstname LIKE @firstname) OR (lastname LIKE @lastname)"

    SelectCommand.Parameters.Add("@firstname", FirstName.Text +  '%');
    SelectCommand.Parameters.Add("@lastname", LastName.Text + '%');

    I think that might work.

    Author Comment

    No, there is no such method available.

    Author Comment

    And the '%' doesn't work even if I hardcode it using the SelectParameters collection.
    LVL 28

    Accepted Solution

    Try "%" instead of '%' in my code
    LVL 7

    Expert Comment

    If you use objectdatasource in 2.0 then you do something like this.

    <asp:ControlParameter DefaultValue="%" Name="firstname" ControlID="Dropdownlist1" Type="String" />

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity mvc C# 2 27
    Syntax question II 2 26
    Session variables expiration ASP.NET C# 3 38
    Whats wrong with this query 21 29
    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now