colliec
colliec

Reputation: 35

Search SqlDatabase using different data ASP.NET C#

Hi i can currently search the sql database using the following code, but i can only search using the Customer_firstname is there a way i can search for the surname as well as the firstname? i have found numerous tutorials and examples but all of the examples only use one datatype for the search.

protected void Button_cus_pmr_Click(object sender, EventArgs e)

    {

        //select all the data from the PMR table where it is equal to the text in the search text box
        string staSearch = "select * FROM [PMR] where (Customer_Firstname like '%" + TextBox_cus_pmr.Text.ToString() + "%)";
        SqlDataSource1.SelectCommand = staSearch;//perform the staSearch query on the SqlDataSource1

    }

Upvotes: 0

Views: 547

Answers (1)

Sam
Sam

Reputation: 5014

You should be able to simply add a second condition for the surname. For example:

string staSearch = "select * FROM [PMR] where (Customer_Firstname like '%" + TextBox_cus_pmr.Text.ToString() + "% OR Customer_Surname like '%" + TextBox_surName.Text.ToString() + "%')";

Replace TextBox_surName with the name of the surname text box and Customer_Surname with the name of the surname database column.

Like Lareau mentioned in a comment, this isn't a very safe way to do queries because a user could run other SQL commands on your server by entering them into the textbox. For more information on ways to avoid this, take a look at this article.

Upvotes: 1

Related Questions